简体   繁体   English

从MySQL检索数据缓慢

[英]Slow data retrieving from Mysql

I want to retrieve some data from MySQL based on my complicated queries written in my PHP codes. 我想根据我用PHP代码编写的复杂查询从MySQL检索一些数据。 The speed of retrieving data is so slow. 检索数据的速度太慢。 How can I solve this problem? 我怎么解决这个问题? Is there any way to improve the performance? 有什么办法可以提高性能? I'm using WAMP Server. 我正在使用WAMP Server。 I need to do it all in PHP, Indeed I want to concatenate all tweets from the same user and push it in an array with his/her name. 我需要在PHP中完成所有操作,的确,我想连接来自同一用户的所有tweet,并将其推入具有他/她名字的数组中。 I have used $twitter array to store the name of twittersand I concatenate all tweets from the same user in dataset like this : 我已经使用$ twitter数组存储twitters的名称,并且将来自同一用户的所有tweet连接到数据集中,如下所示:

$number_of_twitters=sizeof($twitters);

for($i=0;$i $number_of_twitters;$i++){
    $getTweetsQuery="SELECT Tweets FROM tweettable WHERE Name='$twitters[$i]'";
    $tweets=mysql_query($getTweetsQuery);

$tweetString="";

while($tweetsRow=mysql_fetch_array($tweets)){
    $tweetString .= $tweetsRow[0]." ";
}

So why it is so heavy to be performed ? 那么为什么要执行那么繁重的任务呢?

Capture the SQL being issued by your application. 捕获由您的应用程序发出的SQL。 If you can't do it in the app, you could enable the MySQL general log, but that will log ALL sql statements from all sessions. 如果您无法在应用程序中执行此操作,则可以启用MySQL常规日志,但这将记录所有会话中的所有sql语句。

Use EXPLAIN to see the generated access plans. 使用EXPLAIN查看生成的访问计划。

The knee-jerk quick answer is "add indexes", but the key is determining the most appropriate indexes for the queries you are running, and eliminating indexes that are unnecessary. 下意识的快速答案是“添加索引”,但是关键是为正在运行的查询确定最合适的索引,并消除不必要的索引。

There may be predicates in your queries that are preventing optimum performance, for example, wrapping columns in functions disables MySQL from performing index range scans and index seeks. 查询中可能存在妨碍最佳性能的谓词,例如,将列包装在函数中会使MySQL无法执行索引范围扫描和索引查找。

The other knee-jerk quick answer is to add memory to the instance, so that more blocks are cached, to reduce physical I/O. 另一个急于解决的快速答案是向实例添加内存,以便缓存更多块,以减少物理I / O。

The MyISAM storage engine takes table level locks, even for SELECT statements, and that kills concurrency. MyISAM存储引擎即使在SELECT语句中也需要表级锁定,这会杀死并发性。 InnoDB performs much better with concurrent queries. InnoDB在并发查询中的性能要好得多。


UPDATE 更新

The most appropriate index for this query: 此查询最合适的索引:

SELECT Tweets FROM tweettable WHERE Name='$twitters[$i]'

would be a covering index: 将是一个覆盖指数:

... ON tweetable (Name, Tweets)

An EXPLAIN will show the current execution plan: EXPLAIN将显示当前的执行计划:

EXPLAIN SELECT Tweets FROM tweettable WHERE Name='foo'

Ideally, we'd like the explain output to show 'Using index'. 理想情况下,我们希望explain输出显示“使用索引”。 But at a minimum, we want to see MySQL accessing the rows by an index, so it doesn't have to do a full scan of every row in the table to see if the row satisfies the predicate. 但是至少,我们希望看到MySQL通过索引访问行,因此它不必对表中的每一行都进行全面扫描以查看该行是否满足谓词。

It also appears that this query is in a loop. 该查询似乎也处于循环中。 It is usually more efficient to retrieve a set of rows with a single statement, rather than running multiple statements. 通常,使用单个语句而不是运行多个语句来检索一组行更为有效。 (And this is particularly the case when each execution is pulling a small number of rows from a very large table, and the query is doing a full scan of the table.) (尤其是当每次执行都从一个非常大的表中提取少量行并且查询对表进行完整扫描时,尤其如此。)

SELECT t.Tweets
  FROM tweetable t
 WHERE t.Name = 'fee'
    OR t.Name = 'fi'
    OR t.Name = 'fo'
    OR t.Name = 'fum'
 ORDER BY t.Name

or the equivalent: 或等效的:

SELECT t.Tweets
  FROM tweetable t
 WHERE t.Name IN ('fee','fi','fo','fum')
 ORDER BY t.Name

MySQL also has a GROUP_CONCAT function you might find useful. MySQL还有一个GROUP_CONCAT函数,您可能会觉得有用。

SELECT GROUP_CONCAT(t.Tweets ORDER BY t.Name SEPARATOR ' ') AS tweetstring
  FROM tweetable t
 WHERE t.Name IN ('fee','fi','fo','fum')

NOTE The length of the string returned by GROUP_CONCAT is limited by the group_concat_max_len and max_allowed_packet variables. 注意 GROUP_CONCAT返回的字符串的长度受group_concat_max_lenmax_allowed_packet变量限制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM