转自 石展@赶集
别让脚趾头想事情,那是脑瓜子的职责。
md5() / Order by Rand()
INT
不超1000WCHAR
不超500Walter table
快INT
字段CHAR
字段BIGINT
,而非 INT
,后期表结构调整代价巨大;Float
、Double
,MySQL 后续版本将不再支持上述两种类型;DECIMAL
类型,这样性能更好,存储更紧凑。INET_ATON()
、INET_NTOA()
函数转换使用ENUM
占用1字节,转为数值运算SET
视节点定,最多占用8字节'
单引号(即使是数值)'sex' enum('F','M') COMMENT '性别'
'c1' enum('0','1','2','3') COMMENT '职介审核'
NULL
字段
NULL
列加索引,需要额外空间NULL
复合索引无效'a' char(32) DEFAULT NULL
'b' int(10) NOT NULL
'c' int(10) NOT NULL DEFAULT 0
TEXT
类型处理性能远低于VARCHAR
VARCHAR(65535)
==>64K (注意UTF-8)TEXT
/BLOB
数据类型CREATE TABLE t1 (
id INT NOT NULL AUTO_INCREMENT,
data TEXT NOT NULL,
PRIMARY KEY(id)
) ENGINE=InnoDB;
26
26*26*26*26=456,976
26*26*26*26*26=11,881,376
26*26*26*26*26*26=308,915,776
CREATE TABLE test (
blob_col varchar(100) DEFAULT NULL COMMENT '小区拼音',
INDEX(blob_col(10))
)ENGINE=InnoDB;
DELETE FROM t1 where TIMESTAMPDIFF(DAY, modify_time,CURRENT_TIMESTAMP()) > 31
DELETE FROM t1 WHERE modify_time < '31天前的时间'
AUTO_INCREMENT
列或全局ID生成器做代理主键可能一条大SQL就把整个数据库堵死
SELECT *
时
SELECT *
,叧取需要数据列
covering index
提供可能性Select
/JOIN
减少硬盘临时表生成,特别是有TEXT
/BLOB
时or
改写为in()
OR
效率:O(n)
IN
效率:O(Log n)
n
很大时,OR
会慢很多IN
的个数,建议n
小于200Select * from opp WHERE phone='12347856' or phone=‘42242233'
Select * from opp WHERE phone in ('12347856' ,'42242233')
or
改为union
or
查询Merge index
往往很弱智set global optimizer_switch='index_merge=off';
Select * from opp WHERE phone='010-88886666' or cellPhone='13800138000';
Select * from opp WHERE phone='010-88886666' union Select * from opp WHERE cellPhone='13800138000';
NOT
、!=
、<>
、!<
、!>
、NOT EXISTS
、NOT IN
、NOT LIKE
等MySQL> select * from post WHERE title like '北京%' ;
298 rows in set (0.01sec)
MySQL> select * from post WHERE title like '%北京%' ;
572 rows in set (3.27sec)
=
COUNT(1)=
COUNT(1)=
COUNT(100)!=
COUNT(col)Select * from table limit 10000,10;
Select * from table WHERE id>=23423 limit 11; #10+1 (每页10条)
select * from table WHERE id>=23434 limit 11;
Select * from table WHERE id >= ( select id from table limit 10000,1 ) limit 10;
SELECT * FROM table INNER JOIN (SELECT id FROM table LIMIT 10000,10) USING (id) ;
select id from table limit 10000,10;
Select * from table WHERE id in (123,456...) ;
MySQL> select sql_no_cache * from post limit 10,10;
10 row in set (0.01 sec)
MySQL> select sql_no_cache * from post limit 20000,10;
10 row in set (0.13sec)
MySQL> select sql_no_cache * from post limit 80000,10;
10 rows in set (0.58sec)
MySQL> select sql_no_cache id from post limit 80000,10;
10 rows in set (0.02sec)
MySQL> select sql_no_cache * from post WHERE id>=323423 limit 10;
10 rows in set (0.01sec)
MySQL> select * from post WHERE id >= ( select sql_no_cacheid from post limit 80000,1 ) limit 10 ;
10 rows in set (0.02 sec)
UNION ALL
,UNION
有去重开销。JOIN
IN()
MySQL> Select * from tag JOIN tag_poston tag_post.tag_id=tag.idJOIN post on tag_post.post_id=post.id WHERE tag.tag=‘二手玩具’;
MySQL> Select * from tag WHERE tag=‘二手玩具’;
MySQL> Select * from tag_post WHERE tag_id=1321;
MySQL> Select * from post WHERE post.id in (123,456,314,141);
MySQL> select phone, count(*) from post group by phone limit 1;
1 row in set (2.19 sec)
MySQL> select phone, count(*) from post group by phone order by null limit 1;
1 row in set (2.02 sec)
#字段:`remark` varchar(50) NOT NULL COMMENT '备注,默认为空',
MySQL>SELECT `id`, `gift_code` FROM gift WHERE `deal_id` = 640 AND remark=115127;
1 row in set (0.14sec)
MySQL>SELECT `id`, `gift_code` FROM pool_giftWHERE `deal_id` = 640 AND remark='115127';
1 row in set (0.005 sec)
Insert values, values, values
减少索引刷新Load data
比insert
快约20倍update post set tag=1 WHERE id in (1,2,3);
sleep 0.01;
update post set tag=1 WHERE id in (4,5,6);
sleep 0.01;
prod
库pre
库dev
库WHERE
中使用IN
id的子查询JOIN
改写select * from table1 where id in (select id from table2);
insert into table1 (select * from table2);
UTF8
utf8_general_ci
idx_字段名