我们将实现一个Oracle SQL视图表,目前,它使用excessive count(*)来连接另一个表:
SELECT
portfolio.id as id,
portfolio.name as name,
portfolio.number as number,
currency.id as currencyId,
currency.key as currencyIso,
customer.id as cbsId,
(CASE WHEN position_tbl.max_available_balance >0 THEN 'true' ELSE 'false' END) as hasAvailableBalance,
(CASE WHEN wallet_tbl.wallet_count > 0 THEN 'true' ELSE 'false' END) as hasWallet,
(CASE WHEN account_tbl.fiat_account_count > 0 THEN 'true' ELSE 'false' END) as hasFiatAccount,
wallet_tbl.wallet_count,
account_tbl.fiat_account_count
FROM SCHEMA.CUSTOMER customer
INNER JOIN SCHEMA.PORTFOLIO portfolio on portfolio.customer_id = customer.id
LEFT JOIN (
SELECT COUNT(*) as wallet_count,
portfolio_number
FROM SCHEMA.WALLET
GROUP BY portfolio_number) wallet_tbl on wallet_tbl.portfolio_number= portfolio.portfolio_number
LEFT JOIN (
SELECT MAX(position.available_balance) as max_available_balance,
portfolio_number
FROM SCHEMA.POSITION position
GROUP BY position.portfolio_number) position_tbl on position_tbl.portfolio_number =portfolio.portfolio_number
INNER JOIN CURRENCY currency on currency.id = portfolio.reference_currency_id
LEFT JOIN (
SELECT COUNT(*) as fiat_account_count,
portfolio_id
FROM ACCOUNT
GROUP BY portfolio_id) account_tbl on account_tbl.portfolio_id = portfolio.id
有人能建议或建议什么可以优化这里的性能?
1条答案
按热度按时间smdncfj31#
如果是这种情况,那么,1)* 收集所有相关表的统计数据 。如果这还不能解决问题, 试试这些提示 *(在四个地方),看看效果如何: