mysql 300万数据查询500多秒怎么优化啊
linux下 mysql 300万数据查询500多秒怎么优化啊,其中pid已经做索引,id是主键
SELECT id,pid,keyWords,shortUrl FROM keywords WHERE pid=0 ORDER BY id DESC LIMIT 50
explain 如下:
mysql> explain SELECT id,pid,keyWords,shortUrl FROM keywords WHERE pid=0 ORDER BY id DESC LIMIT 50;+----+-------------+----------+------+---------------+------+---------+-------+---------+-----------------------------+| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |+----+-------------+----------+------+---------------+------+---------+-------+---------+-----------------------------+| 1 | SIMPLE | keywords | ref | pid | pid | 4 | const | 2452523 | Using where; Using filesort |+----+-------------+----------+------+---------------+------+---------+-------+---------+-----------------------------+1 row in set (8.18 sec)
另外mysql cpu占用很高怎么回事?内存:512M
配置文件如下:
# Example MySQL config file for small systems.## This is for a system with little memory (<= 64M) where MySQL is only used# from time to time and it's important that the mysqld daemon# doesn't use much resources.## You can copy this file to# /etc/my.cnf to set global options,# mysql-data-dir/my.cnf to set server-specific options (in this# installation this directory is /usr/local/mysql/var) or# ~/.my.cnf to set user-specific options.## In this file, you can use all long options that a program supports.# If you want to know which options a program supports, run the program# with the "--help" option.# The following options will be passed to all MySQL clients[client]#password = your_passwordport = 3306socket = /tmp/mysql.sock# Here follows entries for some specific programs# The MySQL server[mysqld]port = 3306socket = /tmp/mysql.sockskip-lockingkey_buffer = 16Kmax_allowed_packet = 1Mtable_cache = 4sort_buffer_size = 64Kread_buffer_size = 256Kread_rnd_buffer_size = 256Knet_buffer_length = 2Kthread_stack = 64Kdatadir=/www/mysql/datalog-slow-queries=/www/log/mysql/slowquery.loglong_query_time=2# Don't listen on a TCP/IP port at all. This can be a security enhancement,# if all processes that need to connect to mysqld run on the same host.# All interaction with mysqld must be made via Unix sockets or named pipes.# Note that using this option without enabling named pipes on Windows# (using the "enable-named-pipe" option) will render mysqld useless!# #skip-networkingserver-id = 1# Uncomment the following if you want to log updates#log-bin=mysql-bin# Uncomment the following if you are NOT using BDB tables#skip-bdb# Uncomment the following if you are using InnoDB tables#innodb_data_home_dir = /usr/local/mysql/var/#innodb_data_file_path = ibdata1:10M:autoextend#innodb_log_group_home_dir = /usr/local/mysql/var/#innodb_log_arch_dir = /usr/local/mysql/var/# You can set .._buffer_pool_size up to 50 - 80 %# of RAM but beware of setting memory usage too high#innodb_buffer_pool_size = 16M#innodb_additional_mem_pool_size = 2M# Set .._log_file_size to 25 % of buffer pool size#innodb_log_file_size = 5M#innodb_log_buffer_size = 8M#innodb_flush_log_at_trx_commit = 1#innodb_lock_wait_timeout = 50[mysqldump]quickmax_allowed_packet = 16M[mysql]no-auto-rehash# Remove the next comment character if you are not familiar with SQL#safe-updates[isamchk]key_buffer = 8Msort_buffer_size = 8M[myisamchk]key_buffer = 8Msort_buffer_size = 8M[mysqlhotcopy]interactive-timeout
回复讨论(解决方案)
建立pid,id复合索引,PID在复合索引的前面,ID在复合索引号的最后 即可
ls +
看extra 部分,Using filesort是导致性能低下一个很大的原因。
原因是对于pid和id的查找和order by部分没有用上索引。
加上(pid,id的聚合索引)
对关键字段加上索引。
ls +
看extra 部分,Using filesort是导致性能低下一个很大的原因。
原因是对于pid和id的查找和order by部分没有用上索引。
加上(pid,id的聚合索引)
具体命令式是什么呢,新手不太懂哎
建立pid,id复合索引,PID在复合索引的前面,ID在复合索引号的最后 即可
具体命令式是什么呢,新手不太懂哎
ls +
看extra 部分,Using filesort是导致性能低下一个很大的原因。
原因是对于pid和id的查找和order by部分没有用上索引。
加上(pid,id的聚合索引)
创建主键时不就自动创建索引了么
ls +
看extra 部分,Using filesort是导致性能低下一个很大的原因。
原因是对于pid和id的查找和order by部分没有用上索引。
加上(pid,id的聚合索引)
创建主键时不就自动创建索引了么
baidu 主键索引,联合索引
alter table xxx add index pid_id ( pid , id );
你这个可能没建主键索引,否则explain不会这么显示
进入phpmyadmin首页 右侧点击 Show Runtime Information,下面有你表的运行情况,看看那些Value红色的行,然后注意后面的Description,它能帮助你调整mysql到最优化的性能。
另外,表示innodb 还是 myisam?
myisam的话,可以尝试调整一下。
key_buffer = 16K =》 key_buffer = 16M
table_cache = 4 =》 table_cache = 512
sort_buffer_size = 64K =》 sort_buffer_size = 2M
read_rnd_buffer_size = 256K =》 read_rnd_buffer_size = 2M
增加一个 myisam_sort_buffer_size = 16M
然后重启mysqld再看看情况。mysql cpu占用过高不用去理会。
pid已经做索引,id是主键.
为什么会那么慢,没有道理
虽然pid和id都有索引,但一次只能用其一,所以你必须建一个联合索引
做个分区吧。。。。
linux下 mysql 300万数据查询500多秒怎么优化啊,其中pid已经做索引,id是主键
SELECT id,pid,keyWords,shortUrl FROM keywords WHERE pid=0 ORDER BY id DESC LIMIT 50
红色部分语句惹的祸,进行了全表扫描,加索引也木用。
去掉红色部分就好了
测试了一下貌似不行,楼主还是分表吧。
把你语句里的ORDER BY id DESC 去掉,如果你的id是自动编辑的话,在my.cnf里配置一下按 DESC来排序的,然后直接查就好了。
试试联合索引吧
要不,用子查询试试
还有,是不是表的大小大于内存大小了呢?
内存512M,系统还要用掉一些,能留给MYSQL的不多。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Alipay PHP...

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.
