zoukankan      html  css  js  c++  java
  • 常用SQL的优化

    导入数据

    对于MyISAM存储引擎的表,可以通过以下方式快速地导入大量数据

    alter table tbl_name disable keys;              //关闭表非唯一索引的更新

    loading the data

    alter table tbl_name enable keys;

    如果是空表,默认是先导入数据然后才创建索引的,所以不用进行设置。

    对于InnoDB类型的表,这种方式并不能提高导入数据的效率,可以有以下几种方式

    1.InnoDB类型的表示按照主键的顺序来保存的,所以将导入的数据按照主键的顺序排列,可以有效提高导入数据的效率。

    2.在导入数据前执行set unique_checks=0,关闭唯一性校验,结束后执行set unique_checks=1,恢复唯一性校验。

    3.在导入数据前执行set autocommit=0,关闭自动提交,结束后执行set autocommit=1,恢复自动提交。

    优化insert语句

    如果同时从同一客户插入很多行,应尽量使用多个值表的insert语句,比分开执行的单个insert快。

    尽量减少额外的排序,通过索引直接返回有序数据。

    子查询效率不如关联查询,因为不要在内存中创建临时表来完成这个逻辑上需要两个步骤的查询工作。

    例如select * from customer where customer_id not in (select customer_id from payment)

    替换为select * from customer a left join payment b on a.customer_id=b.customer_id where b.customer_id is null

    正则表达式:

      select first_name, email from customer where email regexp "@163[,.]com$"

  • 相关阅读:
    修改 Mac 默认 PHP 运行环境,给 MAMP 配置全局 Composer
    修改 Mac 默认 PHP 运行环境,给 MAMP 配置全局 Composer
    Intellij Idea/Webstorm/Phpstorm 的高效快捷键
    Intellij Idea/Webstorm/Phpstorm 的高效快捷键
    管理工具推荐
    mongo find
    redis 工具
    Redis Quick Start [熟练版]
    Redis cluster
    redis cluster 使用中出现的问题
  • 原文地址:https://www.cnblogs.com/tuifeideyouran/p/4398699.html
Copyright © 2011-2022 走看看