您好,欢迎访问一九零五行业门户网

mysql 300万数据查询500多秒怎么优化啊

本帖最后由 dz215136304 于 2013-08-15 11:33:52 编辑
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的不多。
其它类似信息

推荐信息