【Mysql】公開課之-Query-rewrite
1 啟用或禁用
2 簡單練習
3 去除型別轉換
4 改欄位名後相容報錯
網盤:
- /usr/local/mysql/bin/mysql -uroot -p`cat /etc/sqlpass ` -P3306 -h127.0.0.1 < $BASEDIR/share/install_rewriter.sql
- /usr/local/mysql/bin/mysql -uroot -p`cat /etc/sqlpass ` -P3306 -h127.0.0.1 < $BASEDIR/share/uninstall_rewriter.sql
-
mysql> show variables like 'rewrite_%';
+------------------+-------+
| Variable_name | Value |
+------------------+-------+
| rewriter_enabled | ON |
| rewriter_verbose | 1 |
+------------------+-------+ -
mysql> use query_rewrite;
mysql> show tables;
+-------------------------+
| Tables_in_query_rewrite |
+-------------------------+
| rewrite_rules |
+-------------------------+
1 row in set (0.00 sec)
[mysqld]
rewriter_enabled=ON
2 簡單練習
-
mysql> insert into rewrite_rules(pattern,pattern_database,replacement) values('select ?','test','select ?+1');
mysql> call flush_rewrite_rules(); -
mysql> select 1;
+-----+
| 1+1 |
+-----+
| 2 |
+-----+
3 去除型別轉換
-
mysql> show create table tb\G;
*************************** 1. row ***************************
Table: tb
Create Table: CREATE TABLE `tb` (
`id` int(10) DEFAULT NULL,
`name` varchar(20) DEFAULT NULL,
) ENGINE=InnoDB DEFAULT CHARSET=utf8
mysql> select * from tb;
+------+----------+
| id | name |
+------+----------+
| 1 | 12345678 |
| 1 | 12345671 |
| 1 | 12345672 |
| 1 | 12345673 |
| 1 | 12345674 |
| 1 | 12345675 |
| 1 | 12345676 |
| 1 | 12345677 |
| 1 | 12345679 |
+------+----------+
mysql> explain select * from tb where name=12345677;
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------------+
| 1 | SIMPLE | tb | NULL | ALL | idx_name | NULL | NULL | NULL | 9 | 11.11 | Using where |
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------------+
mysql> insert into rewrite_rules(pattern,pattern_database,replacement) values("select * from tb where name=?","test","select * from tb where name = cast(? as char character set utf8)");
Query OK, 1 row affected (0.08 sec)
思考:為什麼不直接寫?替換為‘?’ ,因為?如果被單引號包起來就失效了!
mysql> call flush_rewrite_rules();
Query OK, 0 rows affected (0.09 sec)
mysql> select * from tb where name=12345678;
mysql> show warnings;
+-------+------+---------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Level | Code | Message |
+-------+------+---------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Note | 1105 | Query 'select * from tb where name=12345678' rewritten to 'select * from tb where name = cast(12345678 as char character set utf8)' by a query rewrite plugin |
+-------+------+---------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------------+
| 1 | SIMPLE | tb | NULL | ALL | idx_name | NULL | NULL | NULL | 9 | 11.11 | Using where |
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------------+
mysql> insert into rewrite_rules(pattern,pattern_database,replacement) values("select * from tb where name=?","test","select * from tb where name = cast(? as char character set utf8)");
Query OK, 1 row affected (0.08 sec)
思考:為什麼不直接寫?替換為‘?’ ,因為?如果被單引號包起來就失效了!
mysql> call flush_rewrite_rules();
Query OK, 0 rows affected (0.09 sec)
mysql> select * from tb where name=12345678;
mysql> show warnings;
+-------+------+---------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Level | Code | Message |
+-------+------+---------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Note | 1105 | Query 'select * from tb where name=12345678' rewritten to 'select * from tb where name = cast(12345678 as char character set utf8)' by a query rewrite plugin |
+-------+------+---------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
4 改欄位名後相容報錯
-
mysql> select * from tb where uid=1;
ERROR 1054 (42S22): Unknown column 'uid' in 'where clause'
-
mysql> insert into rewrite_rules(pattern,pattern_database,replacement) values("select * from tb where uid=?","test","select * from tb where id=?");
Query OK, 1 row affected (0.06 sec)
mysql> call flush_rewrite_rules();
Query OK, 0 rows affected (0.06 sec)
網盤:
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/29096438/viewspace-2129879/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 【Mysql】mysql公開課之-mysql5.7複製特性MySql
- 滴水公開課
- 網易公開課之財務報表分析
- Docker on PowerLinux—— 技術公開課-CSDN公開課-專題視訊課程DockerLinux
- 講課 PPT 公開啦
- 九寶老師公開課第3講:微信公眾號各介面功能演示-CSDN公開課-專題視訊課程
- 經典IT公開課影片講解
- 史丹佛機器學習公開課筆記機器學習筆記
- 【魅族大賽技術公開課】移動應用開發技術精選-CSDN公開課-專題視訊課程
- 微軟線上技術公開課-1月課程預告微軟
- 大學公開課網站影片課程轉碼加密方案網站加密
- 微軟線上技術公開課-12月課程預告微軟
- React Nactive混合APP開發-CSDN公開課-專題視訊課程ReactAPP
- TIB工作室自動化測試實戰公開課【深圳公開課4月、5月】
- 微信小程式開發祕籍-CSDN公開課-專題視訊課程微信小程式
- 開源公開課丨 ChengYing 安裝原理剖析
- 史丹佛iOS Swift開發公開課總結(一)iOSSwift
- 微信公開課:小遊戲設計有“套路”遊戲設計
- 紅帽OpenStack快速入門---分享公開課
- IAA小遊戲公開課,報名啟動!遊戲
- Python高頻面試題解析公開課Python面試題
- 3D遊戲引擎之遊戲攝像機的使用 公開課直播視訊3D遊戲引擎
- 測試公開課資料系列01--Fiddler之AutoResponse線上除錯利器除錯
- “Python Web應用開發框架Django速成”公開課PythonWeb框架Django
- 渡一教育公開課web前端開發JavaScript精英課學習筆記(一)前言Web前端JavaScript筆記
- 初識React:仿網易公開課(react+antd)React
- 「阿里雲 RocketMQ 系列公開課」重磅來襲!阿里MQ
- 阿里雲 EventBridge 系列公開課來襲阿里
- 公開課 | 90分鐘入門人工智慧人工智慧
- Thinking In Android--免費分享公開課ThinkingAndroid
- 測試公開課資料系列02--Postman之chai.js斷言應用PostmanAIJS
- 開源公開課丨 ChunJun 資料傳輸模組介紹
- 聯邦學習、線上信貸、金融安全課程開始報名 | 產業安全公開課聯邦學習產業
- 10小時掌握區塊鏈開發教程-CSDN公開課-專題視訊課程區塊鏈
- 極術社群最受歡迎技術公開課投票啦!參與投票贏取好禮,一鍵觀看所有公開課
- 微信開發之公眾號
- Recode:2013年網路公開課結課率僅為5%
- 開源力量公開課第五十一期-iOS開發入門iOS