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的不多。

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python are both high-level programming languages that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version
Useful JavaScript development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.