如何限制oracle LIKE子句的结果

时间:2015-09-15 19:30:04

标签: sql oracle performance select oracle11g

我有一个拥有2000万用户的Oracle表,
我想查询用户名为“Patel”或“Pat”性能的用户在查询时使用“like子句”非常糟糕。

select * from users  where first name like '%Patel%'  
Or
select * from users  where first name like '%Pat%'

据我所知,如果我将rownum限制结果 - 它只会在LIKE之后发生 - 所以我有一个全表扫描... 我不想扫描整个2000万条记录

select * from users  where first name like '%Pat%' where rownum<100

是否有可能在找到100行后告诉oracle停止?

3 个答案:

答案 0 :(得分:2)

Oracle 12c(最后)引入了this.companyName = Convert.ToString(e.CommandArgument); string upvoteUpdateQuery = "UPDATE dbo.clientDataTable SET upvote = upvote + 1 WHERE compName = @compName"; SqlCommand Upvote = new SqlCommand(upvoteUpdateQuery, voteDb); Upvote.Parameters.AddWithValue("@compName", this.companyName); string insertQuery = "IF CHARINDEX ('voted','@voted') != 8 UPDATE dbo.clientDataTable SET voted += @voted)" + "WHERE compName = @compName ELSE PRINT 'Error, duplicate vote'"; //prevents double voting by seeing if all 8 digits by customer number match, if they don't it works else print //checks if voted customers id exists using charindex, if it doesn't then vote will happen SqlCommand insertVoterDetailsUpvote = new SqlCommand(insertQuery, voteDb); //inserts voter information into specific entries table insertVoterDetailsUpvote.Parameters.AddWithValue("@voted", custVoteTextBox.Text); insertVoterDetailsUpvote.Parameters.AddWithValue("@compName", this.companyName); voteDb.Open(); Upvote.ExecuteNonQuery(); voteDb.Close(); 语法,这应该会更好一点:

fetch first

答案 1 :(得分:2)

  

从名为&#39;%Pat%&#39;的用户中选择*其中rownum&lt; 100

Oracle非常聪明,可以为您做任何事情。此查询的执行计划是:

SELECT STATEMENT, GOAL = ALL_ROWS      
     COUNT STOPKEY          
          TABLE ACCESS FULL  

COUNT STOPKEY意味着当Oracle找到足够的记录来满足条件时,将停止完整扫描。

答案 2 :(得分:1)

由于问题标记为Oracle 11g,我将给出一个适用于11g的答案。

使用first_rows_100的优化程序提示并将其包装到内联视图中。

示例:

bash-3.2$ gdb ./MyTest.53519 -c  ~/public_html/core.20375.MyTest 
GNU gdb (GDB) Red Hat Enterprise Linux (7.0.1-42.el5)
Copyright (C) 2009 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-redhat-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /home/me/tmp/MyTest.53519...done.

warning: core file may not match specified executable file.
[New Thread 20388]
[New Thread 20389]
Reading symbols from /lib64/ld-linux-x86-64.so.2...(no debugging symbols found)...done.
Loaded symbols for /lib64/ld-linux-x86-64.so.2
Core was generated by `./MyTest -np1 -p1'.
Program terminated with signal 11, Segmentation fault.
#0  0x00007f99c19b39e0 in ?? ()
(gdb) bt
#0  0x00007f99c19b39e0 in ?? ()
#1  0x00007f99bc438650 in ?? ()
#2  0x00007f99c19b58cf in ?? ()
#3  0x00007f9994008880 in ?? ()
#4  0x00007f99b8013080 in ?? ()
#5  0x00007f99bc4386f0 in ?? ()
#6  0x00007f99bc438b00 in ?? ()
#7  0x00007f99bc438810 in ?? ()
#8  0x00007f99c19aa489 in ?? ()

此致

奥拉维尔埃