且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用多个左联接优化MySQL查询

更新时间:2022-04-24 22:41:32

  • 确保在WHERE语句和ON条件中的字段上具有索引,默认情况下对主键进行索引,但如果需要,也可以手动创建索引.
  • 创建[UNIQUE | FULLTEXT | SPATIAL] INDEX index_name [index_type] 开启tbl_name(index_col_name,...) [index_type]

    CREATE [UNIQUE|FULLTEXT|SPATIAL] INDEX index_name [index_type] ON tbl_name (index_col_name,...) [index_type]

    index_col_name: col_name [(长度)] [ASC | DESC]

    index_col_name: col_name [(length)] [ASC | DESC]

    index_type: 使用{BTREE |哈希}

    index_type: USING {BTREE | HASH}

    • 检查是否真的必须选择所有表中的每一列?如果不是,请确保仅选择所需的列,避免使用 select *

      仔细检查是否确实需要左联接,如果不需要,请使用INNER JOIN.

      Double check if you really need LEFT JOINS, if no, use INNER JOINs.

      如果在完成查询调整后仍然存在性能问题,请考虑对模式进行规范化以消除联接

      If performance is still an issue after you're done tweaking your query, consider denormalizing your schema to eliminate joins

      您可能还需要考虑通过使用诸如sphinxsearch和memcached之类的缓存应用程序来减少数据库的负载

      You may also want to consider reducing the load on the database by using caching applications like sphinxsearch and memcached

      检查所有联接都不是视图联接,而不是实际表联接

      Check none of your joins are to views rather than actual tables

      参考:

      http://www.sphinxsearch.com

      http://dev.mysql.com/doc/refman/5.0/en/create-index.html