Indexing SQL for Between query with only one match?

后端 未结 3 1779
北恋
北恋 2021-01-04 11:15

We have a table with more than two million rows where all queries against it will be a Between lookup using Column1 and Column2. Also, there will

3条回答
  •  囚心锁ツ
    2021-01-04 11:50

    Are the ranges always non-overlapping? You mention that there is always only one match. If they are, you can write it as:

    SELECT * FROM table1 
       WHERE 8 <= Col2 
       ORDER BY Col2 ASC
       LIMIT 1
    

    This will give you the row with the lowest value of Col2 which is above 8 - which is the range you are interested in. The index would only be needed on Col2, and the cost should be small.

    Since you did not mention the DBMS you are using, the LIMIT 1 should be replaced with whatever your DB uses to fetch the first N results.

    You will have to check Col1 <= your_value in code to ensure that the value you are looking for really is in the range.

提交回复
热议问题