Database optimization

Database optimization

Idea: Optimize the query statement first, then back it up and modify it

1. Try not to use * when querying

2. Query and add an index/foreign key (add the condition item of the query statement directly in navcate)

例:select * from table where age>18 and sex="男"
给age和sex加外键,优先检索前面的age【二叉树最小值】

3. Design reasonable sql query conditions and use and according to the actual situation

4. When using 'or' to query, try not to use "or", try to use "union all"

注:一旦用or就不走索引了,union all 是走索引,先一个表单独查,再做并集

5. Use like fuzzy query (% in the statement)

尽量不要在需要查询的关键字之前加%,因为这样就不走索引了

6. When using count, the most efficient order is count(field name) <count(*) <count(1)

7. In addition to count, average, etc., try not to use the functions that come with sql

因为:自带的函数不走索引

8. Use the sql statement optimization tool: exlain/desc [a set of command tools, you must check the specific usage]

用法:exlain 正常sql语句

9. Database Design Principles

9.1尽量遵循三大范式
    字段类型
        varchar(长度+1) 
            用户名
            描述
            邮箱
            手机号
        char
            手机号
            密码
        int
            性别(int/tinyint) 数字比字符查询速度快
9.2所有字段尽量设计成NotNull
9.3表引擎尽量用Innodb
    Innodb支持事务,MyIsam不支持
    Innodb是行锁,MyIsam是表锁  锁是防止多个sql语相互冲突
    Innodb不支持全文检索,MyIsam支持全文检索

10. For operations with high query frequency, try to use MemCache (third-party database caching tool) and redis (memory database, non-relational database, with its own deduplication)

MemCache 适合不太频繁改变sql语句的查询,位于应用与数据库之间
频繁写入的数据库不适合用关系型数据库,适合用redis - 例:购物车功能、浏览次数统计;注:通常redis和mysql一起使用,redis负责频繁读写的功能

11. Sub-table (consider this last, you need to modify the corresponding query statement)

按列分 - 分离数据项
按行分 - 逻辑分区

12. Multiple libraries, read and write separation

主从设置

13.mysql configuration

14. Hardware configuration

cpu使用率、应用程序所占进程、mysql进程

15: Other

当数据太多时候不要在高峰期添加索引,索引是将字段加到一张索引表,对导致短暂卡顿
时间戳现在是10位或13位(带毫秒),到2030年失效
业务逻辑尽量不要给数据库处理,在程序中处理,逻辑单边化独立化

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324683694&siteId=291194637