MySql数据库查询需要很多时间来处理数据

时间:2019-05-23 07:07:11

标签: php mysql sql

我的桌子上有超过1000万条记录。我想在一个字段上使用分组依据获取数据。

Table structure(users)
--------------
id
user_name,
project_id,
position,
keyword (Reapeated values),
created_date,


$results = 'SELECT * FROM users WHERE id IN ( SELECT MAX(id) FROM users WHERE project_id = 453 GROUP BY keyword ) LIMIT 0, 10';

然后我使用上述查询的结果并遍历每行,例如

foreach ($results as $row) {
    $start = 'SELECT position FROM users WHERE keyword = ' . $row['keyword'] .' AND  project_id = 453 ORDER BY created_date ASC LIMIT 1'; // To get the first record from all the rows.

    $end =  'SELECT position FROM users WHERE keyword = ' . $row['keyword'] .' AND  project_id = 453 ORDER BY created_date DESC LIMIT 1'; // To get the last record from all the rows.

    $best = 'SELECT position FROM users WHERE keyword = ' . $row['keyword'] .' AND  project_id = 453 ORDER BY position ASC LIMIT 1'; // To lowest positon record from all the rows.

    // Consider all above query is execute and store data in respective variables like $start, $end and $best

    $alltimechange = $start['position'] - $end['position'];

    $data[] = [
        'start' => $start['position],
        'best' => $best['position],
        'alltimechange' => $alltimechange,
    ];
}

如何使其更快?

0 个答案:

没有答案