all groups > sql server programming > june 2007 >
You're in the

sql server programming

group:

performance issue


performance issue shanky
6/29/2007 7:34:00 PM
sql server programming:
I have one performance problem....

I am using follwing query to get some data back from sql server 2000...

select @Rec_Cnt = count(Customer_Id)
FROM products with (nolock)
WHERE product1 like COALESCE(@product1, product1) and
product2 like COALESCE(@product2, product2) and
product3 Like COALESCE(@product3, product3) and
product4 Like COALESCE(@product4, product4) and
product5 = COALESCE(@product5, product5) and
((product6 = @product6 ) or (@product6 Is Null)) And
product7 = @product7


I am using count and bunch of like statements...There is no problem in
query...I have around 8 millions rows....it is taking around 2 minutes to run
above query...
what is the best way to improve above query...or there is some other way to
write above code...need to improve the performance. I have index defined on
customerid, product1, product2, product3 and product4.

Thanks
Re: performance issue tallpeak NO[at]SPAM gmail.com
6/29/2007 7:54:14 PM
Your indexes are typically not usable for LIKE clauses. Try text
indexing.

Also, short of text indexing, I would phrase this query more like...

WHERE (@product1 IS NULL OR product1 LIKE @product1)
AND (@product2 IS NULL OR product2 LIKE @product1)
....

Not knowing your data, I wouldn't know what else to suggest, but I
wonder why the LIKE clauses. Do you expect your user to always enter %
signs to search for substrings? Otherwise, the LIKE is more like an =.



[quoted text, click to view]

Re: performance issue shanky
6/29/2007 9:18:01 PM
How do i use text indexing. Do you think Text indexing will reduce the time.
Yes, user can enter any string.....for example for product1 user can enter
'a' or 's'...i need to display back all the products matching with 'a' and
's' ...i do use like 'a%' or 's%'....i will get rid of COALESCE like you
suggested..let see if it makes any difference.....

Thanks for your reply...

[quoted text, click to view]
Re: performance issue Gert-Jan Strik
6/30/2007 12:00:00 AM
For this query, the only usuable index would be an index on
products(product7).

Some general remarks: it is a bit odd that you do "LIKE COALESCE" four
times, "= COALESCE" one time and the equivalent of "= COALESCE" one
time. By the way: changing "prod = COALESCE(@prod,prod)" to "prod =
@prod OR @prod IS NULL" will not make a difference.

What can make a difference is if you rewrite the "LIKE
COALESCE(@prod,prod)" to "LIKE COALESCE(@prod,'%')", but only for
product that you mention first in the index. So I suggest that you do
that for the column where it is most likely that you have filled the
local variable for.

So you are now always using a value in @product7. Let's assume that the
next local variable that is likely to be filled is @product6. In that
case, you could create an index on products(product7,product6), and
rewrite the last two lines of your query to

AND product6 LIKE COALESCE(@product6, '%')
AND product7 = @product7

If the stored procedure is recompiled each time it is called (WITH
RECOMPILE) that you could do this for all combination of product7 +
another productcolumn.

In the end, it will probably still give you just average performance. If
you want better performance, read articles like this:
http://www.sommarskog.se/dynamic_sql.html

HTH,
Gert-Jan


[quoted text, click to view]
Re: performance issue shanky
7/2/2007 6:32:04 AM
Is there a way to just count like 200 rows...instead of doing count on 8
million records....count is taking 2-3 minutes....

[quoted text, click to view]
Re: performance issue Gert-Jan Strik
7/2/2007 7:29:27 PM
Maybe.

You could try something like this:

SELECT @Rec_Cnt = COUNT(Customer_Id)
FROM (
SELECT TOP 200 *
FROM products WITH (nolock)
...
) AS T

But you will still need good indexes, otherwise it will probably not
improve performance. And you need the inner query to use a good index,
and for that you might need Dynamic SQL (the link I posted before).

Gert-Jan


[quoted text, click to view]
AddThis Social Bookmark Button