搜尋
首頁後端開發php教程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的不多。

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
如何防止會話固定攻擊?如何防止會話固定攻擊?Apr 28, 2025 am 12:25 AM

防止會話固定攻擊的有效方法包括:1.在用戶登錄後重新生成會話ID;2.使用安全的會話ID生成算法;3.實施會話超時機制;4.使用HTTPS加密會話數據,這些措施能確保應用在面對會話固定攻擊時堅不可摧。

您如何實施無會話身份驗證?您如何實施無會話身份驗證?Apr 28, 2025 am 12:24 AM

實現無會話身份驗證可以通過使用JSONWebTokens(JWT)來實現,這是一種基於令牌的認證系統,所有的必要信息都存儲在令牌中,無需服務器端會話存儲。 1)使用JWT生成和驗證令牌,2)確保使用HTTPS防止令牌被截獲,3)在客戶端安全存儲令牌,4)在服務器端驗證令牌以防篡改,5)實現令牌撤銷機制,如使用短期訪問令牌和長期刷新令牌。

PHP會議有哪些常見的安全風險?PHP會議有哪些常見的安全風險?Apr 28, 2025 am 12:24 AM

PHP會話的安全風險主要包括會話劫持、會話固定、會話預測和會話中毒。 1.會話劫持可以通過使用HTTPS和保護cookie來防範。 2.會話固定可以通過在用戶登錄前重新生成會話ID來避免。 3.會話預測需要確保會話ID的隨機性和不可預測性。 4.會話中毒可以通過對會話數據進行驗證和過濾來預防。

您如何銷毀PHP會議?您如何銷毀PHP會議?Apr 28, 2025 am 12:16 AM

銷毀PHP會話需要先啟動會話,然後清除數據並銷毀會話文件。 1.使用session_start()啟動會話。 2.用session_unset()清除會話數據。 3.最後用session_destroy()銷毀會話文件,確保數據安全和資源釋放。

如何更改PHP中的默認會話保存路徑?如何更改PHP中的默認會話保存路徑?Apr 28, 2025 am 12:12 AM

如何改變PHP的默認會話保存路徑?可以通過以下步驟實現:在PHP腳本中使用session_save_path('/var/www/sessions');session_start();設置會話保存路徑。在php.ini文件中設置session.save_path="/var/www/sessions"來全局改變會話保存路徑。使用Memcached或Redis存儲會話數據,如ini_set('session.save_handler','memcached');ini_set(

您如何修改PHP會話中存儲的數據?您如何修改PHP會話中存儲的數據?Apr 27, 2025 am 12:23 AM

tomodifyDataNaphPsession,startTheSessionWithSession_start(),然後使用$ _sessionToset,修改,orremovevariables.1)startThesession.2)setthesession.2)使用$ _session.3)setormodifysessessvariables.3)emovervariableswithunset()

舉一個在PHP會話中存儲數組的示例。舉一個在PHP會話中存儲數組的示例。Apr 27, 2025 am 12:20 AM

在PHP會話中可以存儲數組。 1.啟動會話,使用session_start()。 2.創建數組並存儲在$_SESSION中。 3.通過$_SESSION檢索數組。 4.優化會話數據以提升性能。

垃圾收集如何用於PHP會議?垃圾收集如何用於PHP會議?Apr 27, 2025 am 12:19 AM

PHP會話垃圾回收通過概率機制觸發,清理過期會話數據。 1)配置文件中設置觸發概率和會話生命週期;2)可使用cron任務優化高負載應用;3)需平衡垃圾回收頻率與性能,避免數據丟失。

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

PhpStorm Mac 版本

PhpStorm Mac 版本

最新(2018.2.1 )專業的PHP整合開發工具

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

強大的PHP整合開發環境

DVWA

DVWA

Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中

Atom編輯器mac版下載

Atom編輯器mac版下載

最受歡迎的的開源編輯器

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器