mysql 範圍查詢 Mysql 範圍查詢優化

2021-10-25 14:20:46 字數 1557 閱讀 1526

1.2:另外,對於btree索引,index和乙個常量值通過 ,<=,>=,between,!=,或者<>操作符做比較;

1.3: 對於所有型別的index,多範圍條件通過 or and關鍵字組合形式;

'常量值'在之前的描述中意味著:

2.1:查詢字串的常量形式;

2.2: const 或者system表的一列(也只有一列)的自連線(join);

2.4: 有上面型別子表示式完全組成的任意表示式;

where子句範圍查詢的例子:

select * fromt1where key_col > 1

and key_col < 10;select * fromt1where key_col = 1

or key_col in (15,18,20);select * fromt1where key_col like 'ab%'

or key_col between 'bar' and 'foo';

一些非常量值可能在傳播階段轉換為常量;

mysql嘗試對於在where子句中的任何可能的index提取range condition,在提取過程中,不能構造的範圍條件被捨去,產生重疊的條件被組合,產生空範圍的條件被捨去。

如下:select * from t1 where(key1< 'abc' and (key1 like 'abcde%' or key1 like '%b')) or(key1< 'bar' and nonkey = 4) or(key1< 'uux' and key1 > 'z');

key1為index, nonkey非index;

key1的提取過程如下:

1:從原始的where子句開始:

(key1 < 'abc' and (key1 like 'abcde%' or key1 like '%b')) or(key1< 'bar' and nonkey = 4) or(key1< 'uux' and key1 > 'z')

2:捨去nokey = 4和key1 like 『%d』,因為他們不能用作範圍scan,正確的捨去方法是用true代替她們,

(key1 < 'abc' and (key1 like 'abcde%' or true)) or(key1< 'bar' and true) or(key1< 'uux' and key1 > 'z')

3:  以下條件總是true or false;

1:(key1 like 'abcde%' or true) is always true

2:(key1 < 'uux' and key1 > 'z') is always false

(key1 < 'abc' and true) or (key1 < 'bar' and true) or (false)

4: 去掉不必要的true 和false條件:

(key1 < 'abc') or (key1 < 'bar')

5:組合那些重疊的區間成乙個:

(key1 < 'bar')

範圍條件的提取演算法可以處理內嵌的and /or任意深度的構造,他的數去不依賴與他們出現在where子句中的順序;

2:多part index的範圍查詢:

MySQL條件查詢和範圍查詢

一 比較運算子 運算子功能 示例 大於select from student where age 14 小於select from student where age 18 大於等於 select from student where age 15 小於等於 select from student ...

mysql時間範圍查詢 任務詳解 MySQL 基礎

資料庫是計算機系統軟體的基石之一,mysql 則是關聯式資料庫中的經典產品,大量應用基於 mysql 開發。因此對後端碼農來說 mysql 是必須掌握的技能。本任務主要考察 mysql 的基本操作以及初級的索引優化能力。安裝設定 讀者需要在本地環境正確安裝 mysql,並修改相關配置檔案,使得 my...

mysql根據時間範圍查詢

select from bank statistics where day time between 2018 06 01 and 2018 06 10 根據兩端範圍查詢 select from bank statistics where day time 2018 05 24 查詢某一天的 sel...