顯示具有 MYSQL索引 標籤的文章。 顯示所有文章
顯示具有 MYSQL索引 標籤的文章。 顯示所有文章

2019年10月21日 星期一

【MYSQL】記錄一次DBI connect(';;mysql_read_default_group=client','root',...) failed: Can't connect to local MySQL server through socket...報錯-版本5.7

[root@rosalie-mysql01 ~]# pt-duplicate-key-checker -u root -p1234 --database=tttt

DBI connect(';;mysql_read_default_group=client','root',...) failed: Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2) at /usr/local/bin/pt-duplicate-key-checker line 894
顯示預設的sock路徑有誤,重新指定正確路徑即可,加入socket=路徑

加入紫色字段即可
[root@rosalie-mysql01 ~]# pt-duplicate-key-checker -u root -p1234 --database=tttt --socket=/tmp/mysql.sock
# ########################################################################
# tttt.test_index
# ########################################################################

# ID_2 is a duplicate of PRIMARY
# Key definitions:
# KEY `ID_2` (`ID`),
# PRIMARY KEY (`ID`),
# Column types:
# `id` int(11) not null
# To remove this duplicate index, execute:
ALTER TABLE `tttt`.`test_index` DROP INDEX `ID_2`;

# Uniqueness of ID ignored because PRIMARY is a duplicate constraint
# ID is a duplicate of PRIMARY
# Key definitions:
# UNIQUE KEY `ID` (`ID`),
# PRIMARY KEY (`ID`),
# Column types:
# `id` int(11) not null
# To remove this duplicate index, execute:
ALTER TABLE `tttt`.`test_index` DROP INDEX `ID`;

# B is a left-prefix of BCD
# Key definitions:
# KEY `B` (`B`),
# KEY `BCD` (`B`,`C`,`D`),
# Column types:
# `b` int(11) not null
# `c` int(11) not null
# `d` int(11) not null
# To remove this duplicate index, execute:
ALTER TABLE `tttt`.`test_index` DROP INDEX `B`;

# A is a left-prefix of ABC
# Key definitions:
# KEY `A` (`A`),
# KEY `ABC` (`A`,`B`,`C`),
# Column types:
# `a` int(11) not null
# `b` int(11) not null
# `c` int(11) not null
# To remove this duplicate index, execute:
ALTER TABLE `tttt`.`test_index` DROP INDEX `A`;

# ########################################################################
# Summary of indexes
# ########################################################################

# Size Duplicate Indexes 16
# Total Duplicate Indexes 4
# Total Indexes 8

2019年3月27日 星期三

【MYSQL】不走索引的SQL

不走索引的SQL

  • like語句,'%w'不會使用索引,'w%'會使用索引
select * from table_name where name LIKE "%wang%";  -----------------不會使用索引
select * from table_name where name LIKE "wang%";  ------------------會使用索引

  • 列類型為字符串類型字符串類型指(CHAR、VARCHAR、BINARY、VARBINARY、BLOB、TEXT、ENUM和SET),查詢時沒有用單引號引起來
select * from table_name where ship_id=20;  -----------------不會使用索引
select * from table_name where ship_id="20";  ------------------會使用索

  • 在where查詢語句中使用計算試+-*/或是!= , <> 比較
select * from table_name where id +1 = 2103;-----------------不會使用索引

  • 在where查詢語句中對字段進行NULL值判斷(is NULL 或is not NULL)
select * from table_name where group_name=NULL;  -----------------不會使用索引

  • 在where查詢中使用了or關鍵字, myisam表能用到索引, innodb不行;(用UNION替換or,可以使用索引) 
例:where中的indexed欄有建索引、no_index欄沒建索引。
select * from table_name where Indexed="20" or No_index="11";--不會使用索引(一有一沒有建)
select * from table_name where Indexed="20" or Indexed2="11";----會使用索引(兩個都有建)

  • where中組合索引未按順序查詢的index(A欄,B欄,C欄)左前綴法則
  • 但查A,C欄雖有使用到索引,效果沒有ABC來得好,因為只使用到部份索引
select * from table_name where B欄="小熊" and C欄="小Q";--不會使用索引(一定要A欄先)
select * from table_name where A欄="小胖" and C欄="小Q";----會使用索引(A欄有在第一個就可)

  • 如果mysql估計使用全表掃描要比使用索引快,則不使用索引(數據量小時or 索引數據量大於20%的
  • delete中帶in不會走索引
    (查詢table_name2中的id=20符合的,刪除在table_name的資料)
delete from table_name where id in(select id from table_name2 where id=20);--不會使用索引
delete table_name from table_name inner join table_name2 on 
table_name.id=table_name2.id where table_name2=20;--------會使用索引

【MYSQL】MYSQL的SYS表說明(版本8.0)

mysql> use sys Reading table information for completion of table and column names You can turn off this feature to get a quicker s...