1. GROUP BY和PARTITION BY区别

点击前往1

2. 常用语句记录

  1. -- 查询指定数据库的所有表
  2. select *
  3. from information_schema.tables
  4. where table_schema = 'schema-name'
  5. -- 查询指定数据库下的指定表的列信息
  6. select TABLE_NAME 表名,
  7. COLUMN_NAME 字段英文名,
  8. COLUMN_COMMENT 字段中文名,
  9. data_type 类型,
  10. CHARACTER_MAXIMUM_LENGTH 长度
  11. from information_schema.COLUMNS
  12. where TABLE_SCHEMA = 'schema-name' and TABLE_NAME='TABLE_NAME';
  13. #查看表索引
  14. show index from xiot_dev_prod;
  15. #添加唯一索引 using hash没有用
  16. ALTER TABLE `xiot_device`.`xiot_dev_prod`
  17. ADD UNIQUE INDEX `prod_no_unindex`(`prod_no`) USING HASH;
  18. #修改索引
  19. ALTER TABLE `xiot_device`.`xiot_dev_prod`
  20. DROP INDEX `prod_name_index`,
  21. ADD INDEX `prod_name_index`(`prod_name`) USING HASH;

3. exist和in的区别

exists详解
EXISTS和IN的性能分析

4. explain使用记录

查看表索引
image.png

  1. show index from xiot_dev_prod;
  2. ALTER TABLE `xiot_device`.`xiot_dev_prod`
  3. ADD UNIQUE INDEX `prod_no_unindex`(`prod_no`) USING HASH;
  4. #all 全表扫描,没有用到索引
  5. explain
  6. select *
  7. from xiot_dev_prod;
  8. #const 主键索引 常量查询
  9. explain
  10. select *
  11. from xiot_dev_prod
  12. where prod_id = 15;
  13. #const 唯一索引 常量查询
  14. explain
  15. select *
  16. from xiot_dev_prod
  17. where prod_no = 'Inverter';
  18. #range 主键索引,between and 范围查询
  19. explain
  20. select *
  21. from xiot_dev_prod
  22. where prod_id between 20 and 30;
  23. #range 主键索引,in 范围查找
  24. explain
  25. select *
  26. from xiot_dev_prod
  27. where prod_id in (11, 20, 40, 50);
  28. ##range 唯一索引,in范围查找
  29. explain
  30. select *
  31. from xiot_dev_prod
  32. where prod_no in ('AerobicPool', 'Breaker', 'WaterSoftener');
  33. #all 全表扫描,字符串匹配数字,调用了函数没有使用索引
  34. explain
  35. select *
  36. from xiot_dev_prod
  37. where prod_no in (12);
  38. #index 使用索引 查询索引字段
  39. explain
  40. select prod_id
  41. from xiot_dev_prod;
  42. #index 使用唯一索引 查询索引字段
  43. explain
  44. select prod_no
  45. from xiot_dev_prod;
  46. #all 全表查询,查询字段包含为未建立索引的字段
  47. explain
  48. select prod_no, prod_name
  49. from xiot_dev_prod;
  50. #prod:all全表查询,没有用到索引
  51. #prodType:eq_ref 使用主键索引
  52. explain
  53. select *
  54. from xiot_dev_prod prod
  55. join xiot_dev_prod_type prodType on prod.prod_type_id = prodType.prod_type_id;
  56. #range 使用唯一索引 右模糊查询
  57. explain
  58. select *
  59. from xiot_dev_prod
  60. where prod_no like 'i%';
  61. #all 不使用索引 左模糊查询
  62. explain
  63. select *
  64. from xiot_dev_prod
  65. where prod_no like '%ui';
  66. #ref 使用非唯一索引
  67. explain
  68. select *
  69. from xiot_dev_prod
  70. where prod_name = '2';