What is the best optimization technique for a wildcard search through 100,000 records in sql table

本小妞迷上赌 提交于 2019-12-06 03:19:04

Run SQL Profiler and do a tuning profile. This will make recommendations on indexes to execute against your database.

Also, a query such as the following would be worth a try.

SELECT  *
FROM    
( 
    SELECT    ROW_NUMBER() OVER ( ORDER BY ColumnA) AS RowNumber, itemCode, itemName, ItemDesc
    FROM      tblItems
    WHERE     itemName LIKE '%FooBar%'
) AS RowResults
WHERE   RowNumber >= 1 AND RowNumber < 50
ORDER BY RowNumber

EDIT: Updated query to reflect your real scenario.

How about having a search without the leading wildcard as your primary search....

Where itemName like 'SearchWord%'

and then have having a "More Results" button that loads

Where itemName like '%SearchWord%'

(alternatively exclude results from the first result set)

Where itemName not like 'SearchWord%' and itemName like '%SearchWord%'

A weird alternative which might work, as it depends on several assumptions etc. Sorry not fully explained but am using ipad so hard to type. (and yes, this solution has been used in high txn commericial systems)

This assumes

  1. That your query is cpu constrained not IO
  2. That itemName is not too long, such that it holds all letters and numbers
  3. That searchword, in total, contains enough selective characters and isnt just highly common characters
  4. Your selection predicates are constrained by a %like%

The basic idea is to expand your query to help the optimiser know which rows need the like scanning.

Step 1. Setup your table

Create an additional 26 or 36 columns for each letter/digit. When I've done this for real it has always been a seperate table, but putting it on source table should be ok for a small volume like 100k. Lets call the colmns trig_a, trig_b etc.

Create a trigger for each insert/edit/delete and put a 1 or 0 into the trig_a field if it contains an 'a', do this for all 26/36 columns. The trigger to do this is complex, but possible (at least using Oracle). If you get stuck I'm sure SO'ers can create it, or I can dig it out.

At this point, we have a series of columns that indicate whether a field contains a letter/digit etc.

Step 2. Helping you query

With this extra info, we are in the position to help the optimiser. Add the following to your query

Select ... Where .... And
 ((trig_a > 0) or (searchword not like '%a%')) and
 ((trig_b > 0) or (searchword not like '%b%')) and
   ... Repeat for all columns monitored...

If the optimiser behaves, it can use the (hopefully) lower cost field>0 predicates to reduce the like predicates evaluated.

Notes.

  1. You may need to force the optimiser to scan trig_? Fields first
  2. Indexes can help on trig_? Fields, especically if in the source table
  3. I haven't shown how to handle upper/lower case, dont forget to handle this
  4. You might find just doing a few letters is all you need to do.
  5. This technique doesnt offer performance gains for every use of like, so it isnt a general purpose technique for everywhere you use a like.
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!