Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
sanjnep
Posting Yak Master
191 Posts |
Posted - 2006-05-12 : 11:36:28
|
| In table PolicyRetrievals:Select policytype, count (policytype) Total from PolicyRetrievals group by policytype/*policytype Total ----------- ----------- TT 25427AA 7608525AR 8CA 349205SL 406371SS 34274*/When I ran this:Select top 50 * from PolicyRetrievals where policytype = 'AR' order by operreckey desc There is non clustered index on operreckey and when I put policytypeother than AR I got result immediately and it uses the non clustered index on operreckey but when I put AR it will take long time and it doesn't use that index. Even I try to put index on policytype it doesn’t help. There are 83,00,000 rows on the column. Help me to find solution:Thanks Sanjeev OHSanjeev Shrestha12/17/1963 |
|
|
Kristen
Test
22859 Posts |
Posted - 2006-05-12 : 12:15:22
|
| Presumably there is a statistics that says that AR is a very rare value for policytype, therefore using the operreckey index is going to mean a VERY large number of index reads, followed by data reads, whereas just reading the Data directly will be faster (in terms of finding the 8 records) and then sorting them will be trivial.For other records with lots of hits, and given that you have specified only the TOP 50, it makes more sense for SQL to access via the operreckey index - otherwise ALL the matching records must be found, and THEN sorted, and THEN just the TOP 50 selected.An index on policytype would help for this query, but would be useless for all the other values (given that it would not be selective for them - UNLESS you could cover the query from that index - but if you genuinely need to do SELECT * that won't be the case)So ... if its a regular requirement you'll have to find another route - such as keeping a lookup-list of the AR records in another table.Worth making sure that the statistics are up to date (or set to update automatically)Kristen |
 |
|
|
sanjnep
Posting Yak Master
191 Posts |
Posted - 2006-05-12 : 12:54:21
|
| Hi Kristen,Thanks for reply.Actually I need:SELECT TOP 50 Insdstcd,PaymentMessageKey,policytype,operreckey FROM PolicyRetrievalsORDER BY operreckey DESCI need to do various combination of where clause like:where PaymentMessageKey = 'CancelPaymentInformation' AND Insdstcd = 'CA' AND policytype = 'AA'or PaymentMessageKey = 'CancelPaymentInformation' AND policytype = 'AA' etc.statistics for PaymentMessageKey:PaymentMessageKey TotalCountCC 7852537CancelPaymentInformation 98914NCRenewalText 5775QuoteLapsePaymentInformation 127153QuotePaymentInformation 285568UpfrontCancelPaymentInformation 1628statistics for Insdstcd:Insdstcd TotalCountNA 25959AA 66AE 217AK 20008AL 125444AP 233AR 44397AZ 181746BC 1CA 537265CO 120169CT 138135DC 11269DE 36266FL 905815GA 541119HI 36567IA 84044ID 30906IL 120803IN 188493KS 57748KY 114654LA 166482MA 1869MD 157143ME 50964MI 245138MN 160728MO 123835MS 93481MT 15409NC 173274ND 14530NE 39870NH 35273NJ 15461NM 53529NV 87047NY 544406OH 1122274OK 84289ON 8OR 121252PA 420482PQ 17PR 145RI 62212SC 96001SD 24227TN 87127TX 570811UT 43370VA 255466VI 73VT 26644WA 133207WI 89342WV 14199WY 18458-------How to create proper index? I tried various combination but couldn't success:ThanksSanjeev Shrestha12/17/1963 |
 |
|
|
|
|
|
|
|