0

我正在尝试从过去 10 天提高此查询的性能,但它不起作用。主要是该order by子句需要时间来执行,因为当我执行查询时删除它需要几秒钟,但如果包含 order by 那么它需要超过 5 分钟。以下是查询-

Select a.user_vchr_char2, 
       a.voucher_id, 
       a.uap_clm_pymnt_stat, 
       a.pymnt_id_ref, 
       to_char(cast(a.lastupddttm as timestamp), 'YYYY-MM-DD-HH24.MI.SS.'),   
       to_char(a.pymnt_dt,'YYYY-MM-DD'),to_char(a.cancel_dt,'YYYY-MM-DD'),   
       to_char(a.scheduled_pay_dt,'YYYY-MM-DD'), to_char(a.transaction_date,'YYYY-MM-DD'),         
       a.descr254_mixed, 
       a.multiple_flg, 
       a.name1, a.name2, a.address1, a.address2, a.address3,     
       a.address4,a.city, a.state, a.postal, a.country, a.pymnt_method, a.gross_amt,  
       a.currency_pymnt 
FROM table_name a 
where  to_char(a.LASTUPDDTTM,'YYYY-MM-DD HH24:MI:SS') > :1 
order by a.user_vchr_char2, a.pymnt_cnt, a.uap_clm_pymnt_stat

我尝试index在使用的表中添加order by但没有明显改善。谁能帮忙。请简单,因为我对这个领域完全陌生。

谢谢!

4

3 回答 3

0

I recommend you to use a temp table.

IF EXISTS(SELECT name FROM tempdb.sys.objects WHERE name='##temp_table')
BEGIN
    DROP TABLE ##temp_table
END

Select a.user_vchr_char2, a.voucher_id, a.uap_clm_pymnt_stat, a.pymnt_id_ref, to_char(cast(a.lastupddttm as timestamp), 'YYYY-MM-DD-HH24.MI.SS.'), to_char(a.pymnt_dt,'YYYY-MM-DD'),to_char(a.cancel_dt,'YYYY-MM-DD'), to_char(a.scheduled_pay_dt,'YYYY-MM-DD'), to_char(a.transaction_date,'YYYY-MM-DD'), a.descr254_mixed, a.multiple_flg, a.name1, a.name2, a.address1, a.address2, a.address3, a.address4,a.city, a.state, a.postal, a.country, a.pymnt_method, a.gross_amt, a.currency_pymnt
INTO ##temp_table
FROM table_name a 
where to_char(a.LASTUPDDTTM,'YYYY-MM-DD HH24:MI:SS') > 1

And then you can use order by clause like this.

SELECT *
FROM ##temp_table
order by a.user_vchr_char2, a.pymnt_cnt, a.uap_clm_pymnt_stat
于 2013-03-29T13:08:53.720 回答
0

first if your index was not function based then it wont be used. Do an explain plan and you can verify.

a function based index in oracle is specified like this.

create index foo on table_name(to_char(a.LASTUPDDTTM,'YYYY-MM-DD HH24:MI:SS'));

next since there are so many character editing functions I suggest using index organized table This moves the data to the index so it does not require several lookups to load the row data saving time. That is the fastest index you can do in oracle.

create table table_name(a int, b int, c varchar2(20 byte))organization index tablespace users   
pcthreshold 20 including a,b

here a and b at end of create clause map to the index columns. order by does internal sort so if table is parallel this is a huge hit. alternative to get magnitude of performance for general indexes add

parallel 4 (or how many parallel index queries you want)

example

create index foo on table_Name(to_char(a.LASTUPDDTTM,'YYYY-MM-DD HH24:MI:SS')) parallel 4 

Note that partitioning can help but also slow it down if global indexes are used. This is only used for tables with large amounts of rows (ex. 10,000,000 rows). Oracle performance suffers after 1 million rows in a table. Not sure how many rows are in your table.

于 2013-03-29T19:07:52.133 回答
0

与 order by 的问题无关,但您可以通过在 where 子句中不使用函数来提高性能。而不是这个:

where  to_char(a.LASTUPDDTTM,'YYYY-MM-DD HH24:MI:SS') > :1 

寻找一种方法来做到这一点:

where a.LastUpdDtTm >= a start date
于 2013-03-29T13:30:09.290 回答