首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >优化从select查询中选择的查询

优化从select查询中选择的查询
EN

Stack Overflow用户
提问于 2022-09-09 11:44:53
回答 1查看 23关注 0票数 0

我正试图从我的应用程序中清点所有可疑的存款。

查询基本上是从内部select查询中选择、计数和分组结果。

使用此查询:

代码语言:javascript
复制
SELECT *, count(id) as repeated from (
    SELECT id, userAccount_id, `change_value_amount`,`change_value_currency_code`, JSON_EXTRACT(details, '$.depositId') as depositId From billing.transactions WHERE type_value = 'DEPOSIT_MADE'
) as q1 
where depositId is not null group by depositId having repeated > 1

现在,这个查询的执行时间是4-6秒,这真的很烦人。

我还尝试创建一个临时表,并根据temp表执行外层where子句:

代码语言:javascript
复制
create temporary table if not exists susDeposit as (SELECT id, userAccount_id, `change_value_amount`,`change_value_currency_code`, JSON_EXTRACT(details, '$.depositId') as depositId From billing.transactions WHERE type_value = 'DEPOSIT_MADE');

select id, userAccount_id, change_value_amount, change_value_currency_code, depositId, count(*) as repeated  from susDeposit
where depositId is not null group by depositId having repeated > 1

在工作台中,它以最大1秒的速度执行。

但是使用pdo时,它在5秒时执行,并且询问工作台和pdo中执行时间不同的原因。

守则是:

代码语言:javascript
复制
$tempTable = "CREATE TEMPORARY TABLE IF NOT EXISTS susDeposit as (SELECT id, userAccount_id, `change_value_amount`,`change_value_currency_code`, JSON_EXTRACT(details, '$.depositId') as depositId From billing.transactions WHERE type_value = 'DEPOSIT_MADE');";
        $finalSelect = "SELECT id, userAccount_id, change_value_amount, change_value_currency_code, depositId, count(*) as repeated  from susDeposit
        where depositId is not null group by depositId having repeated > 1";

        $query = $this->pdo->query($tempTable);
        $query = $this->pdo->query($finalSelect);
        $query->execute();
        $r = $query->fetch();

有任何方法来优化这个查询吗?

EN

回答 1

Stack Overflow用户

发布于 2022-09-09 22:38:30

首先,不要隐藏您需要在JSON字符串中测试的列。

也就是说,在构建表时,将depositId放在自己的列中,以帮助优化器执行查询。

一旦您完成了这些更改并提供了SHOW CREATE TABLE,我将帮助您创建一个合适的复合索引(如果需要的话)。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73661655

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档