我的 SQL 查询包含大量 ltrim() 、 rtrim() 、 substring() 等转换函数...它们显着影响查询性能...请回答我的以下查询:
- 如何在我的查询中最小化转换函数的使用?
- 除了可以在查询中使用的转换函数之外,还有哪些可能的替代方法?
- 替代方案会显着提高我的查询性能吗?
...请指导我
How do you know these functions are consuning time in execution? have you run it without these functions and noticed that it is quicker?
Are all the columns in the where clause indexed?
If you look at the query plan can you see table scans?
How many rows are in this table?
You could create a new column in your table that contains cleaned up data out of the emp_info column. i.e.
UPDATE YourTable SET NewColumn = ltrim(rtrim(substr(emp_info, 1, 9)))
Then add a trigger to your table to ensure it is always up to date
Then add an index to the column.
Then alter your select to use the new column
You might see some performance improvement. It's impossible to say based on the information you've given.
您可以在获取输入时在前端处理这些转换函数,例如 ltrim、Rtrim、Substring。这些类型的字符串函数在前端也可用。例如:Trim(),Substring()
ltrim(rtrim(
您可以通过替换为节省一点时间trim(
例子:
create table test1(a varchar2(4000));
--Create 1 million rows
--This is hopefully a "large" amount of rows, but small enough to fit
--in memory. This way we can test the CPU time to process the rows
--instead of the IO time to read the data.
begin
for i in 1 .. 10 loop
insert into test1 select ' '||level||' '
from dual connect by level <= 100000;
end loop;
commit;
end;
/
--Note: You'll want to run the statements several times first to
--"warm up" the database, and get everything in memory.
--0.33 seconds
select count(*)
from test1
where ltrim(rtrim(a)) = '5';
--0.20 seconds
select count(*)
from test1
where trim(a) = '5';
但这对于处理 100 万行来说是非常少的时间。
您的系统处理 200 万行需要多长时间?你的数据有什么不寻常的地方吗?例如,您是否使用大型 CLOB 而不是小型 VARCHAR2?您确定问题不是由创建不同解释计划的谓词引起的吗?例如,Oracle 可能认为函数返回的行数明显少于实际返回的行数,因此使用嵌套循环而不是哈希连接。