| Author |
Topic |
|
bornecw
Starting Member
6 Posts |
Posted - 2010-05-06 : 15:59:22
|
| Hello,I have a query that I am trying to imporve the speed of. select 0, p.product_id, p.handheld_desc,coalesce(ps.unit1qty_on_hand, 0), ps.unit1qty_physical, Convert(Int, p.product_id) as SORTID from product p left join product_stock ps on '8547' = ps.loc_id and p.product_id = ps.product_idorder by SORTIDIf I make that:select 0, p.product_id, p.handheld_desc,coalesce(ps.unit1qty_on_hand, 0), ps.unit1qty_physical, Convert(Int, p.product_id) as SORTID from product p left join product_stock ps on p.product_id = ps.product_idwhere '8547' = ps.loc_id order by SORTIDthe results are different. Can you explain to me the difference? Also, any optimization tips would be appreciated.Clay Borne |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
bornecw
Starting Member
6 Posts |
Posted - 2010-05-06 : 18:04:49
|
| There are 700 rows in product and 300 rows in product_stockJust running:select count(*)from product p left join product_stock ps on '8547' = ps.loc_id and p.product_id = ps.product_idtakes 1 minute. ps.loc_id, ps.product_id, and p.product_id are indexed. What else can I do to speed this up?Clay Borne |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
bornecw
Starting Member
6 Posts |
Posted - 2010-05-07 : 08:56:41
|
| Tara,It's strange, the table is destroyed, created, records imported, indexes built, and compacted every day. I have a script that builds indexes:create unique index uq_product_stock on product_stock (loc_id, product_id)But the query was running like there were not any indexes. So I destroyed the uq_product_stock, and did this:create unique index uq_product_id on product_stock (product_id)create unique index uq_loc_id on product_stock (loc_id)After that the query came back in 1 sec as opposed to 1 min. Can you provide any insights into this? I'm not entirely familiar with indexes with multiple fields. Thanks!Clay Borne |
 |
|
|
GilaMonster
Master Smack Fu Yak Hacker
4507 Posts |
Posted - 2010-05-07 : 09:55:50
|
| is ps.locID an int column? If so, remove the quotes around the value in the query and put the index back to the 2-column (it's more useful for this query). I'm guessing that you had implicit conversion which completely prevents index seeks. By splitting the index, you allow SQL to seek on the join column. However removing the implicit conversion and putting back the 2-column index should be even better.--Gail ShawSQL Server MVP |
 |
|
|
bornecw
Starting Member
6 Posts |
Posted - 2010-05-07 : 10:18:05
|
| Thanks for responding,ps.locID is a nvarchar(6) column. Would removing the implicit conversion still help? If so, how do you remove it?Clay Borne |
 |
|
|
bornecw
Starting Member
6 Posts |
Posted - 2010-05-07 : 10:19:30
|
| BTW this is Sql Compact 3.5Clay Borne |
 |
|
|
GilaMonster
Master Smack Fu Yak Hacker
4507 Posts |
Posted - 2010-05-07 : 11:07:59
|
| No, if it's a nvarchar, leave the quotes.I know nothing about compact edition, so can't help any further. It would have been useful if you'd stated that in your initial post.--Gail ShawSQL Server MVP |
 |
|
|
bornecw
Starting Member
6 Posts |
Posted - 2010-05-07 : 14:04:35
|
| Thank you, I learned from you, so don't fret.Clay Borne |
 |
|
|
|