主要记录阅读High Performance MySQL后新发现的知识点。
1.uuid用binary保存
建议uuid不要使用char来保存,而用binary(16)来保存。这里在长度上来讲用binary会节省一半。因为一个字符占用1个字节,而一个字节实际上可以表示0-256(2^8),用16进制的表示需要2个字节00-FF(0-256)。
优化前:SET uuid = UUID() (类型:char(36))
优化后:SET uuid = HEX(REPLACE(UUID(), '-', '')) (类型:binary(16))
2.用crc32替换长字符串的查找
如果索引列是个很长的字符串,例如url。那可以再建立一个列用来保存这个列的crc32结果,以提高索引的使用速度。
优化前:WHERE url = 'http://willko.iteye.com/' (索引:url,类型:var/char(?))
优化后:WHERE url_crc32 = CRC32('http://willko.iteye.com/') AND url = 'http://willko.iteye.com/' (索引:url_crc32,类型:unsigned int)
3.前缀索引和后缀索引
前缀索引听得比较多,优点是减少索引的长度,缺点是排序不能使用前缀索引(影响distinct/order/group),也不会出现Covering Index(只读取索引就能满足查询)。
后缀索引还是首次听到,孤陋寡闻了。因为MySQL不支持反向索引,所有有时候查询会有问题,例如字段blog保存用户的博客地址(http://willko.iteye.com),那需要查询某个域名有多少个用户就不好查询,可以用一个额外的字段反转保存。blog_reverse:moc.eyeavaj.willko://ptth,这样就很容易查到iteye.com(moc.eyeavaj)有多少用户了,并可以使用索引,也就是解决了 LIKE '%?'的问题,因为查询反转成LIKE '?%'了。
4.散列数据
散列数据就是把原本只有一条记录的散列成多条,充分利用InnoDB行锁的特性,提高并发。
例如,之前是UPDATE hit_counter SET cnt = cnt + 1 WHERE id = ?
散列后是 UPDATE hit_counter SET cnt = cnt + 1 WHERE id = ? AND slot = rand() * 100
散列后查询需要合并数据。
5.优化limit和offset
MySQL的limit工作原理就是先读取n条记录,然后抛弃前n条,读m条想要的,所以n越大,性能会越差。
优化前SQL: SELECT * FROM member ORDER BY last_active LIMIT 50,5
优化后SQL: SELECT * FROM member INNER JOIN (SELECT member_id FROM member ORDER BY last_active LIMIT 50, 5) USING (member_id)
分别在于,优化前的SQL需要更多I/O浪费,因为先读索引,再读数据,然后抛弃无需的行。而优化后的SQL(子查询那条)只读索引(Cover index)就可以了,然后通过member_id读取需要的列。
分享到:
相关推荐
解决启动dubbo项目的时候出现,无法读取方案文档 'http://code.alibabatech.com/schema/dubbo/dubbo.xsd',其实在你本地把dubbo.jar文件解压,然后在META-INF下边就有个dubbo.xsd,就是他
<?xml version="1.0" encoding="UTF-8" standalone="no"?> <xsd:schema ... xmlns:beans=... <xsd:import namespace="http://www.springframework.org/schema/tool"/>
mysql-schema-ts 将MySQL模式转换为TypeScript接口 Postgres实施: : 用法 # to infer an entire schema $ npx mysql-schema-ts mysql://root@localhost:3306/database # to infer a specific table $ npx mysql-...
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop=...
下载一个dubbo.xsd文件windows->preferrence->xml->xmlcatalog add->catalog entry ->file system 选择刚刚下载的文件路径 修改key值和配置文件的http://code.alibabatech.com/schema/dubbo/dubbo.xsd 相同 保存即可...
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans ...
<?xml version="1.0" encoding="UTF-8"?><beansxmlns=...
- schema_reference.4: Failed to read schema document 'http://code.alibabatech.com/schema/dubbo/dubbo.xsd', because 1) could not find the document; 2) the document could not be read; 3) the root ...
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context=...
xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context=...
- schema_reference.4: Failed to read schema document 'http://code.alibabatech.com/schema/dubbo/dubbo.xsd', because 1) could not find the document; 2) the document could not be read; 3) the root ...
这是一个springmvc-config.xml文件,<?xml version="1.0" encoding="UTF-8"?> ... xmlns:mvc=... http://www.springframework.org/schema/context/spring-context-4.2.xsd">
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd ...
基于 mysql-schema-sync 开发的,从一个db同步table数据到另一个db的工具
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop=...
eclispe配置dubbo,本地需要有dubbo.xsd,对应的Key:http://code.alibabatech.com/schema/dubbo/dubbo.xsd
”http://www.springframework.org/schema/beans” xmlns:xsi=”http://w3.org/2001/XMLSchema-instance” xmlns:aop=” http://www.springframework.org/schema/aop” xmlns:tx=” ...
mysql-schema-sync 是一款使用 Go 开发跨平台的 MySQL 表结构自动同步工具。主要用于解决多个环境数据库表结构不同步问题。
xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:tx=...
MySQL中information_schema是什么