简体   繁体   English

更快的SQL数据库查询

[英]Faster sql query for Oracle database

I have a oracle database with over 2 million rows and 200 columns. 我有一个超过200万行和200列的oracle数据库。 I'm trying to query data in five columns where one of the columns is equal to the most recent date. 我正在尝试查询五列中的数据,其中一列等于最近的日期。 This query below works but seems to be taking long (over 2 min) to process. 下面的查询有效但似乎需要很长时间(超过2分钟)来处理。 Is there a different logic I can use to speed up the query? 我可以使用不同的逻辑来加速查询吗?

SELECT a,b,c,date,e FROM table a WHERE a.date = (SELECT MAX(date) FROM table)

try rownum 尝试rownum

SELECT * FROM (
SELECT A,B,C,DATE,E FROM TABLE A WHERE A.DATE   DESC
)
WHERE ROWNUM=1

you can also use another similar solution 您也可以使用其他类似的解决方案

with  tbl1 as 
(SELECT A,B,C,DATE,E,
       first_value(date) over( order by date desc) maxdate
   FROM TABLE A)
select A,B,C,DATE,E from tbl1 where date = maxdate

YES !!! 是的!!!

Set Index "date" to your table as I_TABLE_DATE, after that change your query to this 将索引“日期”设置为I_TABLE_DATE,然后将查询更改为此

SELECT --+ index_desc(a I_TABLE_DATE)
    a,b,c,date,e 
FROM table a 
WHERE a.date = (SELECT --+ index_desc(b I_TABLE_DATE)
                b.Date
                FROM table b
                where Rownum = 1)

it will be more faster because, during getting maximum date there will be only scanning index by descening, and your main query will work by index ascening and you dont need scan all table 它会更快,因为在获得最大日期期间将只有下降扫描索引,并且您的主查询将通过索引提升工作,您不需要扫描所有表

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

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