Home Database Mysql Tutorial 关于MYSQL索引优化和in or替换为union all的问题_MySQL

关于MYSQL索引优化和in or替换为union all的问题_MySQL

Jun 01, 2016 pm 01:42 PM

bitsCN.com
一个文章库,里面有两个表:category和article。category里面有10条分类数据。article里面有 20万条。

article里面有一个"article_category"字段是与category里的"category_id"字段相对应的。 article表里面已经把 article_category字义为了索引。数据库大小为1.3G。
 问题描述:
 执行一个很普通的查询: Select * FROM `article` Where article_category=11 orDER BY article_id DESC LIMIT 5 。执行时间大约要5秒左右 解决方案:
 建一个索引:create index idx_u on article (article_category,article_id);Select * FROM `article` Where article_category=11 orDER BY article_id DESC LIMIT 5 减少到0.0027秒     继续问题:Select * FROM `article` Where article_category IN (2,3) orDER BY article_id DESC LIMIT 5 执行时间要11.2850秒。
 使用OR:select * from articlewhere article_category=2or article_category=3order by article_id desclimit 5执行时间:11.0777   解决方案:避免使用in 或者 or (or会导致扫表),使用union all 使用UNION ALL:(select * from article where article_category=2 order by article_id desc limit 5)UNION ALL (select * from article where article_category=3 order by article_id desc limit 5)orDER BY article_id desclimit 5执行时间:0.0261
  作者 adparking bitsCN.com

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Reduce the use of MySQL memory in Docker Reduce the use of MySQL memory in Docker Mar 04, 2025 pm 03:52 PM

Reduce the use of MySQL memory in Docker

How do you alter a table in MySQL using the ALTER TABLE statement? How do you alter a table in MySQL using the ALTER TABLE statement? Mar 19, 2025 pm 03:51 PM

How do you alter a table in MySQL using the ALTER TABLE statement?

How to solve the problem of mysql cannot open shared library How to solve the problem of mysql cannot open shared library Mar 04, 2025 pm 04:01 PM

How to solve the problem of mysql cannot open shared library

What is SQLite? Comprehensive overview What is SQLite? Comprehensive overview Mar 04, 2025 pm 03:55 PM

What is SQLite? Comprehensive overview

Run MySQl in Linux (with/without podman container with phpmyadmin) Run MySQl in Linux (with/without podman container with phpmyadmin) Mar 04, 2025 pm 03:54 PM

Run MySQl in Linux (with/without podman container with phpmyadmin)

Running multiple MySQL versions on MacOS: A step-by-step guide Running multiple MySQL versions on MacOS: A step-by-step guide Mar 04, 2025 pm 03:49 PM

Running multiple MySQL versions on MacOS: A step-by-step guide

How do I secure MySQL against common vulnerabilities (SQL injection, brute-force attacks)? How do I secure MySQL against common vulnerabilities (SQL injection, brute-force attacks)? Mar 18, 2025 pm 12:00 PM

How do I secure MySQL against common vulnerabilities (SQL injection, brute-force attacks)?

What are some popular MySQL GUI tools (e.g., MySQL Workbench, phpMyAdmin)? What are some popular MySQL GUI tools (e.g., MySQL Workbench, phpMyAdmin)? Mar 21, 2025 pm 06:28 PM

What are some popular MySQL GUI tools (e.g., MySQL Workbench, phpMyAdmin)?

See all articles