linux下 mysql 300万数据查询500多秒怎么优化啊,其中pid已经做索引,id是主键
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)
# 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的不多。