紧急求救,数据库执行like语句效率问题 oracle下,单个表数据量200万多,查询一个count语句后面的条件是like,执行时间是15s,这正常吗?各位大虾,紧急求救,谢啦 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 select count(*) from table_name where 字段1 = '111111'请教请教 select count(*) from table_name where 字段1 = '111111'这句sql换成select count(1) from table_name where 字段1 = '111111' 效率应该会稍微高点的呢 select count(*) from table_name where 字段1 = '111111'* 表示所有的字段 只需写表的主键就行了 我写错了 实际上是 select count(1) from 表 where 字段 like '_%1111' scott@ORCL> create table t(id,owner,object_id) as select rownum,owner,object_id 2 from all_objects nologging;Table created.scott@ORCL> select count(*) from t; COUNT(*)---------- 11446scott@ORCL> create index i_t_object_id on t(object_id);Index created.scott@ORCL> exec dbms_stats.gather_table_stats('SCOTT','T',cascade=>true);PL/SQL procedure successfully completed.scott@ORCL> select count(*) from t where object_id like '_%1111'; COUNT(*)---------- 1scott@ORCL> select * from table(dbms_xplan.display_cursor()); -->使用了索引快速扫描 ,性能应该可以啊,看看你的语句的执行计划 PLAN_TABLE_OUTPUT-------------------------------------------------------------------------------------------SQL_ID dfmgyvm7087hj, child number 0-------------------------------------select count(*) from t where object_id like '_%1111'Plan hash value: 3095383276---------------------------------------------------------------------------------------| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |---------------------------------------------------------------------------------------| 0 | SELECT STATEMENT | | | | 8 (100)| || 1 | SORT AGGREGATE | | 1 | 5 | | ||* 2 | INDEX FAST FULL SCAN| I_T_OBJECT_ID | 572 | 2860 | 8 (13)| 00:00:01 |---------------------------------------------------------------------------------------Predicate Information (identified by operation id):--------------------------------------------------- 2 - filter(TO_CHAR("OBJECT_ID") LIKE '_%1111')19 rows selected. scott@ORCL> select count(*) from t where object_id like '_%1111';Statistics---------------------------------------------------------- 0 recursive calls 0 db block gets 30 consistent gets --count(*) 30 0 physical reads 0 redo size 411 bytes sent via SQL*Net to client 385 bytes received via SQL*Net from client 2 SQL*Net roundtrips to/from client 0 sorts (memory) 0 sorts (disk) 1 rows processedscott@ORCL> select count(1) from t where object_id like '_%1111';Statistics---------------------------------------------------------- 138 recursive calls 0 db block gets 57 consistent gets -- >count(1) 57 0 physical reads 0 redo size 411 bytes sent via SQL*Net to client 385 bytes received via SQL*Net from client 2 SQL*Net roundtrips to/from client 4 sorts (memory) 0 sorts (disk) 1 rows processedscott@ORCL> /Statistics---------------------------------------------------------- 138 recursive calls 0 db block gets 57 consistent gets -- >count(1) 57 0 physical reads 0 redo size 411 bytes sent via SQL*Net to client 385 bytes received via SQL*Net from client 2 SQL*Net roundtrips to/from client 4 sorts (memory) 0 sorts (disk) 1 rows processedscott@ORCL> select count(*) from t where object_id like '_%1111';Statistics---------------------------------------------------------- 0 recursive calls 0 db block gets 30 consistent gets --count(*) 30 0 physical reads 0 redo size 411 bytes sent via SQL*Net to client 385 bytes received via SQL*Net from client 2 SQL*Net roundtrips to/from client 0 sorts (memory) 0 sorts (disk) 1 rows processed不见得count(*) 比 count(1)高效啊 select count(主键) from t where object_id like '_%1111';like 在使用索引时。like '1111%'这样更好 like '_%1111'情况下可能不走索引,like '1111%'情况下可能没问题 sql*plus更改用户 用户不存在? Oracle中nvarchar2长度是多少? 表与表之間的比較 数据库搞应用开发这一块初学看什么书好? 如何找到oracle的bug database? 这么简单的函数也出错。就1行代码啊。 急问存储过程在VC++中调用??谢谢! 请大家来讨论:关于ORACLE iDS,iAS. 知道多少说多少。 sql translate 空字符问题 Oracle口令文件的作用,如何创建和使用口令文件 哪位大神知道怎么用oracle实现负载均衡而不需要磁盘阵列
请教请教
这句sql换成select count(1) from table_name where 字段1 = '111111' 效率应该会稍微高点的呢
* 表示所有的字段 只需写表的主键就行了
scott@ORCL> create table t(id,owner,object_id) as select rownum,owner,object_id
2 from all_objects nologging;Table created.scott@ORCL> select count(*) from t; COUNT(*)
----------
11446scott@ORCL> create index i_t_object_id on t(object_id);Index created.scott@ORCL> exec dbms_stats.gather_table_stats('SCOTT','T',cascade=>true);PL/SQL procedure successfully completed.scott@ORCL> select count(*) from t where object_id like '_%1111'; COUNT(*)
----------
1scott@ORCL> select * from table(dbms_xplan.display_cursor()); -->使用了索引快速扫描 ,性能应该可以啊,看看你的语句的执行计划 PLAN_TABLE_OUTPUT
-------------------------------------------------------------------------------------------
SQL_ID dfmgyvm7087hj, child number 0
-------------------------------------
select count(*) from t where object_id like '_%1111'Plan hash value: 3095383276---------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
---------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | | | 8 (100)| |
| 1 | SORT AGGREGATE | | 1 | 5 | | |
|* 2 | INDEX FAST FULL SCAN| I_T_OBJECT_ID | 572 | 2860 | 8 (13)| 00:00:01 |
---------------------------------------------------------------------------------------Predicate Information (identified by operation id):
--------------------------------------------------- 2 - filter(TO_CHAR("OBJECT_ID") LIKE '_%1111')
19 rows selected.
scott@ORCL> select count(*) from t where object_id like '_%1111';
Statistics
----------------------------------------------------------
0 recursive calls
0 db block gets
30 consistent gets --count(*) 30
0 physical reads
0 redo size
411 bytes sent via SQL*Net to client
385 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1 rows processedscott@ORCL> select count(1) from t where object_id like '_%1111';
Statistics
----------------------------------------------------------
138 recursive calls
0 db block gets
57 consistent gets -- >count(1) 57
0 physical reads
0 redo size
411 bytes sent via SQL*Net to client
385 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
4 sorts (memory)
0 sorts (disk)
1 rows processedscott@ORCL> /
Statistics
----------------------------------------------------------
138 recursive calls
0 db block gets
57 consistent gets -- >count(1) 57
0 physical reads
0 redo size
411 bytes sent via SQL*Net to client
385 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
4 sorts (memory)
0 sorts (disk)
1 rows processedscott@ORCL> select count(*) from t where object_id like '_%1111';
Statistics
----------------------------------------------------------
0 recursive calls
0 db block gets
30 consistent gets --count(*) 30
0 physical reads
0 redo size
411 bytes sent via SQL*Net to client
385 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1 rows processed不见得count(*) 比 count(1)高效啊
like 在使用索引时。like '1111%'这样更好