提交 edac95c8 编写于 作者: martianzhang's avatar martianzhang

index advisor skip columns in if clause

	select * from actor where actor_id = 1 order by if(first_name="PENELOPE", last_name, "") desc
上级 08ca16c6
......@@ -279,7 +279,8 @@ func FindEQColsInWhere(node sqlparser.SQLNode) []*common.Column {
err := sqlparser.Walk(func(node sqlparser.SQLNode) (kontinue bool, err error) {
switch node := node.(type) {
// 对AST中所有节点进行扫描
case *sqlparser.Subquery, *sqlparser.JoinTableExpr, *sqlparser.BinaryExpr, *sqlparser.OrExpr:
case *sqlparser.Subquery, *sqlparser.JoinTableExpr, *sqlparser.BinaryExpr, *sqlparser.OrExpr,
*sqlparser.CaseExpr, *sqlparser.FuncExpr:
// 忽略子查询,join condition,数值计算,or condition
return false, nil
......
......@@ -87,6 +87,7 @@ func TestFindCondition(t *testing.T) {
common.Log.Debug("Entering function: %s", common.GetFunctionName())
sqls := []string{
`SELECT * FROM film WHERE length % 20 = 4;`,
`select * from actor where actor_id = 1 order by if(first_name="PENELOPE", last_name, "") desc`,
}
for _, sql := range append(sqls, common.TestSQLs...) {
fmt.Println(sql)
......
......@@ -39,6 +39,7 @@ test-dsn:
allow-old-passwords: false
disable: false
allow-online-as-test: true
disable-version-check: false
drop-test-temporary: true
cleanup-test-database: false
only-syntax-check: false
......
......@@ -39,6 +39,7 @@ test-dsn:
allow-old-passwords: true
disable: false
allow-online-as-test: true
disable-version-check: false
drop-test-temporary: false
cleanup-test-database: true
only-syntax-check: true
......
......@@ -2455,7 +2455,7 @@ FROM
| 1 | PRIMARY | *b* | NULL | eq\_ref | PRIMARY | PRIMARY | 2 | sakila.a.country\_id | 1 | ☠️ **100.00%** | O(log n) | NULL |
| 2 | UNION | *b* | NULL | ALL | NULL | NULL | NULL | NULL | 109 | ☠️ **100.00%** | ☠️ **O(n)** | NULL |
| 2 | UNION | *a* | NULL | ref | idx\_fk\_country\_id | idx\_fk\_country\_id | 2 | sakila.b.country\_id | 5 | ☠️ **100.00%** | O(log n) | NULL |
| 3 | UNION RESULT | *<union1,2>* | NULL | ALL | NULL | NULL | NULL | NULL | 0 | n% | ☠️ **O(n)** | Using temporary |
| 2 | UNION | *a* | NULL | ref | idx\_fk\_country\_id | idx\_fk\_country\_id | 2 | sakila.b.country\_id | 5 | ☠️ **100.00%** | O(log n) | NULL |
......@@ -2465,8 +2465,6 @@ FROM
* **PRIMARY**: 最外层的select.
* **UNION RESULT**: UNION查询的结果集.
* **UNION**: UNION中的第二个或后面的SELECT查询, 不依赖于外部查询的结果集.
#### Type信息解读
......@@ -2477,10 +2475,6 @@ FROM
* ☠️ **ALL**: 最坏的情况, 从头到尾全表扫描.
#### Extra信息解读
* ☠️ **Using temporary**: 表示MySQL在对查询结果排序时使用临时表. 常见于排序order by和分组查询group by.
## 建议使用 AS 关键字显示声明一个别名
......@@ -2537,7 +2531,7 @@ WHERE
| 1 | PRIMARY | *a* | NULL | ref | idx\_fk\_country\_id | idx\_fk\_country\_id | 2 | sakila.b.country\_id | 5 | n% | O(log n) | Using where; Not exists |
| 2 | UNION | *a* | NULL | ALL | NULL | NULL | NULL | NULL | 600 | ☠️ **100.00%** | ☠️ **O(n)** | NULL |
| 2 | UNION | *b* | NULL | eq\_ref | PRIMARY | PRIMARY | 2 | sakila.a.country\_id | 1 | n% | O(log n) | Using where; Not exists |
| 3 | UNION RESULT | *<union1,2>* | NULL | ALL | NULL | NULL | NULL | NULL | 0 | n% | ☠️ **O(n)** | Using temporary |
| 2 | UNION | *b* | NULL | eq\_ref | PRIMARY | PRIMARY | 2 | sakila.a.country\_id | 1 | n% | O(log n) | Using where; Not exists |
......@@ -2547,8 +2541,6 @@ WHERE
* **PRIMARY**: 最外层的select.
* **UNION RESULT**: UNION查询的结果集.
* **UNION**: UNION中的第二个或后面的SELECT查询, 不依赖于外部查询的结果集.
#### Type信息解读
......@@ -2565,8 +2557,6 @@ WHERE
* **Using where**: WHERE条件用于筛选出与下一个表匹配的数据然后返回给客户端. 除非故意做的全表扫描, 否则连接类型是ALL或者是index, 且在Extra列的值中没有Using Where, 则该查询可能是有问题的.
* ☠️ **Using temporary**: 表示MySQL在对查询结果排序时使用临时表. 常见于排序order by和分组查询group by.
## 为sakila库的city表添加索引
......@@ -3827,7 +3817,7 @@ FROM
| 1 | PRIMARY | *o* | NULL | eq\_ref | PRIMARY | PRIMARY | 2 | sakila.i.city\_id | 1 | ☠️ **100.00%** | O(log n) | NULL |
| 2 | UNION | *o* | NULL | ALL | NULL | NULL | NULL | NULL | 109 | ☠️ **100.00%** | ☠️ **O(n)** | NULL |
| 2 | UNION | *i* | NULL | eq\_ref | PRIMARY | PRIMARY | 2 | sakila.o.country\_id | 1 | ☠️ **100.00%** | O(log n) | NULL |
| 3 | UNION RESULT | *<union1,2>* | NULL | ALL | NULL | NULL | NULL | NULL | 0 | n% | ☠️ **O(n)** | Using temporary |
| 2 | UNION | *i* | NULL | eq\_ref | PRIMARY | PRIMARY | 2 | sakila.o.country\_id | 1 | ☠️ **100.00%** | O(log n) | NULL |
......@@ -3837,8 +3827,6 @@ FROM
* **PRIMARY**: 最外层的select.
* **UNION RESULT**: UNION查询的结果集.
* **UNION**: UNION中的第二个或后面的SELECT查询, 不依赖于外部查询的结果集.
#### Type信息解读
......@@ -3847,10 +3835,6 @@ FROM
* ☠️ **ALL**: 最坏的情况, 从头到尾全表扫描.
#### Extra信息解读
* ☠️ **Using temporary**: 表示MySQL在对查询结果排序时使用临时表. 常见于排序order by和分组查询group by.
## 建议使用 AS 关键字显示声明一个别名
......@@ -4001,7 +3985,7 @@ WHERE
| 1 | PRIMARY | *o* | NULL | eq\_ref | PRIMARY | PRIMARY | 2 | sakila.i.city\_id | 1 | ☠️ **100.00%** | O(log n) | Using where; Not exists |
| 2 | UNION | *o* | NULL | ALL | NULL | NULL | NULL | NULL | 109 | ☠️ **100.00%** | ☠️ **O(n)** | NULL |
| 2 | UNION | *i* | NULL | eq\_ref | PRIMARY | PRIMARY | 2 | sakila.o.country\_id | 1 | ☠️ **100.00%** | O(log n) | Using where; Not exists |
| 3 | UNION RESULT | *<union1,2>* | NULL | ALL | NULL | NULL | NULL | NULL | 0 | n% | ☠️ **O(n)** | Using temporary |
| 2 | UNION | *i* | NULL | eq\_ref | PRIMARY | PRIMARY | 2 | sakila.o.country\_id | 1 | ☠️ **100.00%** | O(log n) | Using where; Not exists |
......@@ -4011,8 +3995,6 @@ WHERE
* **PRIMARY**: 最外层的select.
* **UNION RESULT**: UNION查询的结果集.
* **UNION**: UNION中的第二个或后面的SELECT查询, 不依赖于外部查询的结果集.
#### Type信息解读
......@@ -4027,8 +4009,6 @@ WHERE
* **Using where**: WHERE条件用于筛选出与下一个表匹配的数据然后返回给客户端. 除非故意做的全表扫描, 否则连接类型是ALL或者是index, 且在Extra列的值中没有Using Where, 则该查询可能是有问题的.
* ☠️ **Using temporary**: 表示MySQL在对查询结果排序时使用临时表. 常见于排序order by和分组查询group by.
## 建议使用 AS 关键字显示声明一个别名
......
......@@ -39,6 +39,7 @@ test-dsn:
allow-old-passwords: false
disable: false
allow-online-as-test: false
disable-version-check: false
drop-test-temporary: true
cleanup-test-database: false
only-syntax-check: false
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册