473,378 Members | 1,396 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Stored procedure won't use index

Hi,

We're using SQL Server 2000.

A very basic query on a 5 million row table would not work. If we
hard coded the one input parameter, the query used the foreign key
index and produced an instantaneous result. If, however, we pass the
parameter in as a parameter, the query doesn't use the index and takes
forever. E.g.

Declare @ID int
SET @ID = 17697

Select top 1 AccountID from tblAccounts where GroupID = @ID

We have fixed the problem by using an index hint to force the query to
use the index. However, my concern is - why is the index not
automatically used when we remove the hardcoded ID and repalce it with
a parameter? Is it a problem with the index? We do not use index
hints as a coding standard, so will this happen to other stored
procedures in our DB?

Any advice about how and why this occurs would be much appreciated.

Trudie
Jul 20 '05 #1
3 5211
Did you create the index after the stored procedure was created? You
can try to rebuild the SP using the with recompile option. This way
the query plan is not being cached and rebuild every time you call the
SP
Jul 20 '05 #2
The index was created before the SP

Trudie

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 20 '05 #3
[posted and mailed, please reply in news]

Trudie (tr****@sparkdata.co.uk) writes:
We're using SQL Server 2000.

A very basic query on a 5 million row table would not work. If we
hard coded the one input parameter, the query used the foreign key
index and produced an instantaneous result. If, however, we pass the
parameter in as a parameter, the query doesn't use the index and takes
forever. E.g.

Declare @ID int
SET @ID = 17697

Select top 1 AccountID from tblAccounts where GroupID = @ID


This is a little unclear. In the narrative, you talk about a parameter,
but the example there is a plain variable. And, yes, that makes a
difference.

In the snippet above, SQL Server has no idea of what value @ID has,
so it will have to make some general assumption. If the value of
GroupID is skewed, so that 4.5 half million rows all have 0 in
the column, using the index is a very poor idea, in case @ID is 0.

When you have a parameter to a stored procedure, SQL Server does build
the query plan from the value the parameter has the first time you
call the procedure, and that plan is then cached. So if you first call
it with some value is that is too frequent to permit an Index Seek,
you will get a table scan also when you pass values that are more
selective.

Even when you call the procedure with a good value the first time, I
suspect that the optimizer does not build a plan based on an index,
if most other values are bad for the index.

--
Erland Sommarskog, SQL Server MVP, so****@algonet.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 20 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Bill Kellaway | last post by:
Hi there - this should be fairly simple for someone. Basically I can't figure out how to pass the parameters from ASP to a Stored Procedure on SQL. Here's my code: I just need to help in...
5
by: Warren Wright | last post by:
Hi group, I have a select statement that if run against a 1 million record database directly in query analyzer takes less than 1 second. However, if I execute the select statement in a stored...
7
by: Alex Vorobiev | last post by:
hi there, i am using sql server 7. below is the stored procedure that is giving me grief. its purpose it two-fold, depending on how it is called: either to return a pageset (based on page...
10
by: Thomas R. Hummel | last post by:
I have a stored procedure that suddenly started performing horribly. The query plan didn't look right to me, so I copy/pasted the code and ran it (it's a single SELECT statement). That ran pretty...
4
by: Guru | last post by:
Hi All I am using a Global Temporary table in the Stored procedure and i am creating index for a column in that temporary table.When i am executing it. It is not taking that index. I checked...
7
by: JIM.H. | last post by:
Hello, Is there any difference to between SLQ string in the code and call execute query and call a stored procedure and execute the query that way concerning speed, effectiveness, reliability,...
28
by: mooreit | last post by:
The purpose for my questions is accessing these technologies from applications. I develop both applications and databases. Working with Microsoft C#.NET and Microsoft SQL Server 2000 Production and...
6
by: Bonzol | last post by:
Hey there, I think this is not actually VB, but the language im using is VB. Ive been doing all my code,, NOT using Stored procedures.. so my functions are like.(in 2003) visual basic...
9
by: Jonathan Wood | last post by:
I've written a stored procedure and would like to filter the results returned based on the argument. It works so far, but I'd also like to allow this argument to be null. I know I can rewrite my...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.