简体   繁体   English

MySQL 查询耗时 2 分钟

[英]MySQL query takes 2 minutes

I have a problem.我有个问题。 I am running a MySQL PhpMyAdmin server and I have 3 tables:我正在运行 MySQL PhpMyAdmin 服务器,我有 3 个表:

  • CandlestickData烛台数据
  • CandlestickDataHist1烛台数据Hist1
  • CandlestickDataHist2烛台数据Hist2

Then I created a few with the following source query:然后我使用以下源查询创建了一些:

SELECT
    a.*
FROM
    (
    SELECT
        *,
        'CandlestickData' AS SOURCE
    FROM
        CandlestickData
    UNION
SELECT
    *,
    'CandlestickDataHist1' AS SOURCE
FROM
    CandlestickDataHist1
UNION
SELECT
    *,
    'CandlestickDataHist2' AS SOURCE
FROM
    CandlestickDataHist2
) AS a
ORDER BY
    a.MainKey
DESC

This gives me a view with 1.7 million records.这让我看到了 170 万条记录。 When I do the following simple query on the view:当我对视图执行以下简单查询时:

SELECT * FROM my_created_view;

It takes arround 2 minutes to execute the query.执行查询大约需要 2 分钟。 Is there a way to make it faster?有没有办法让它更快?

You can speed it up a bit using UNION ALL rather than UNION :您可以使用UNION ALL而不是UNION加快速度:

SELECT cd.*
FROM ((SELECT cd.*, 'CandlestickData' AS SOURCE
       FROM CandlestickData cd
      ) UNION ALL
      (SELECT cd.*, 'CandlestickDataHist1' AS SOURCE
       FROM CandlestickDataHist1 cd
      ) UNION ALL
      (SELECT cd.*, 'CandlestickDataHist2' AS SOURCE
       FROM CandlestickDataHist2 cd
      )
     ) cd
ORDER BY cd.MainKey DESC;

This will still require sorting all the data.这仍然需要对所有数据进行排序。 However, it will remove the overhead for removing duplicates.但是,它将消除删除重复项的开销。 I might venture that it would be about 50% faster.我可能会冒险说它会快 50%。

If you really want to speed the query, you need to store all the data in a single table.如果您真的想加快查询速度,则需要将所有数据存储在一个表中。 Then you can create an index for the order by key.然后您可以order by创建索引。

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

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