eygle.com   eygle.com
eygle.com  
 

« February 20, 2006 | Blog首页 | February 22, 2006 »



February 21, 2006

使用Index提示 强制使用索引

作者:eygle

出处:http://blog.eygle.com

虽然索引并不总会快于全表扫描,但是很多时候我们希望Oracle使用索引来执行某些SQL,这时候我们可以通过index hints来强制SQL使用index.

Index Hints的格式如下:

/*+ INDEX ( table [index [index]...] ) */

我们简单看一下这个提示的用法(范例为Oracle10g数据库):

SQL> create table t as select username,password from dba_users;
Table created.
SQL> create index i_t on t(username);
Index created.
SQL> set autotrace trace explain
SQL> select /*+ index(t i_t) */ * from t where username='EYGLE';
Execution Plan
----------------------------------------------------------
Plan hash value: 2928007915
------------------------------------------------------------------------------------
| Id  | Operation                   | Name | Rows  | Bytes | Cost (%CPU)| Time     |
------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT            |      |     1 |    34 |     2   (0)| 00:00:01 |
|   1 |  TABLE ACCESS BY INDEX ROWID| T    |     1 |    34 |     2   (0)| 00:00:01 |
|*  2 |   INDEX RANGE SCAN          | I_T  |     1 |       |     1   (0)| 00:00:01 |
------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
   2 - access("USERNAME"='EYGLE')
Note
-----
   - dynamic sampling used for this statement 

这里的查询使用了索引.

需要注意的是使用CTAS方式创建数据表,新建表会继承原表的约束属性:

SQL> desc t
 Name                                      Null?    Type
 ----------------------------------------- -------- ----------------------------
 USERNAME                                  NOT NULL VARCHAR2(30)
 PASSWORD                                           VARCHAR2(30) 

 

如果不使用Hints,此处Oracle不会使用索引:

SQL> select * from t where username='EYGLE';
Execution Plan
----------------------------------------------------------
Plan hash value: 1601196873
--------------------------------------------------------------------------
| Id  | Operation         | Name | Rows  | Bytes | Cost (%CPU)| Time     |
--------------------------------------------------------------------------
|   0 | SELECT STATEMENT  |      |     1 |    34 |     2   (0)| 00:00:01 |
|*  1 |  TABLE ACCESS FULL| T    |     1 |    34 |     2   (0)| 00:00:01 |
--------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
   1 - filter("USERNAME"='EYGLE')
Note
-----
   - dynamic sampling used for this statement 

索引和全表扫描的选择和取舍并非简单,本文不作进一步探讨.

Posted by eygle at 10:44 PM | Comments (5)


如何才能防止拉网页?

作者:eygle

出处:http://blog.eygle.com

今天网站[www.eygle.com]的服务器又经历了一次考验. 上午发现CPU idle 变为0,主机开始满负荷运转,前端访问巨慢,赶紧登陆检查。

在apache日志中发现大量如下日志:

grep "Web Downloader/6.3" access_log.20060221 |awk '{print $1 " " $12$13}'
61.145.165.xx "WebDownloader/6.3"
61.145.165.xx "WebDownloader/6.3"
61.145.165.xx "WebDownloader/6.3"
61.145.165.xx "WebDownloader/6.3"
61.145.165.xx "WebDownloader/6.3"
61.145.165.xx "WebDownloader/6.3" 

原来是有人在使用WebDownloader工具拉我的网站。

赶快封了该地址,流量和负载一下就降了下来:

iptables -A INPUT -s 61.145.165.xx -j REJECT 

可是怎样才能防止这种拉网页的行为呢?

谁有好办法请指点一下:)

也请访问我网站的朋友手下留情,不要这样来拉网页,我的服务器很脆弱的。

 

Posted by eygle at 4:25 PM | Comments (6)



CopyRight © 2004-2008 eygle.com, All rights reserved.