1. GROUP BY和PARTITION BY区别
2. 常用语句记录
-- 查询指定数据库的所有表select *from information_schema.tableswhere table_schema = 'schema-name'-- 查询指定数据库下的指定表的列信息select TABLE_NAME 表名,COLUMN_NAME 字段英文名,COLUMN_COMMENT 字段中文名,data_type 类型,CHARACTER_MAXIMUM_LENGTH 长度from information_schema.COLUMNSwhere TABLE_SCHEMA = 'schema-name' and TABLE_NAME='TABLE_NAME';#查看表索引show index from xiot_dev_prod;#添加唯一索引 using hash没有用ALTER TABLE `xiot_device`.`xiot_dev_prod`ADD UNIQUE INDEX `prod_no_unindex`(`prod_no`) USING HASH;#修改索引ALTER TABLE `xiot_device`.`xiot_dev_prod`DROP INDEX `prod_name_index`,ADD INDEX `prod_name_index`(`prod_name`) USING HASH;
3. exist和in的区别
4. explain使用记录
查看表索引
show index from xiot_dev_prod;ALTER TABLE `xiot_device`.`xiot_dev_prod`ADD UNIQUE INDEX `prod_no_unindex`(`prod_no`) USING HASH;#all 全表扫描,没有用到索引explainselect *from xiot_dev_prod;#const 主键索引 常量查询explainselect *from xiot_dev_prodwhere prod_id = 15;#const 唯一索引 常量查询explainselect *from xiot_dev_prodwhere prod_no = 'Inverter';#range 主键索引,between and 范围查询explainselect *from xiot_dev_prodwhere prod_id between 20 and 30;#range 主键索引,in 范围查找explainselect *from xiot_dev_prodwhere prod_id in (11, 20, 40, 50);##range 唯一索引,in范围查找explainselect *from xiot_dev_prodwhere prod_no in ('AerobicPool', 'Breaker', 'WaterSoftener');#all 全表扫描,字符串匹配数字,调用了函数没有使用索引explainselect *from xiot_dev_prodwhere prod_no in (12);#index 使用索引 查询索引字段explainselect prod_idfrom xiot_dev_prod;#index 使用唯一索引 查询索引字段explainselect prod_nofrom xiot_dev_prod;#all 全表查询,查询字段包含为未建立索引的字段explainselect prod_no, prod_namefrom xiot_dev_prod;#prod:all全表查询,没有用到索引#prodType:eq_ref 使用主键索引explainselect *from xiot_dev_prod prodjoin xiot_dev_prod_type prodType on prod.prod_type_id = prodType.prod_type_id;#range 使用唯一索引 右模糊查询explainselect *from xiot_dev_prodwhere prod_no like 'i%';#all 不使用索引 左模糊查询explainselect *from xiot_dev_prodwhere prod_no like '%ui';#ref 使用非唯一索引explainselect *from xiot_dev_prodwhere prod_name = '2';
