[20150508]列顺序问题.txt

[20150508]列顺序问题.txt

--链接:
https://viveklsharma.wordpress.com/2015/04/30/cpu-cycles-for-column-skipping/

--测试列顺序对CPU cost的影响:

SCOTT@test> @ver1
PORT_STRING                    VERSION        BANNER
------------------------------ -------------- --------------------------------------------------------------------------------
x86_64/Linux 2.4.xx            11.2.0.3.0     Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production

create table T1 as
select     level ID, mod(level,2) N1, mod(level,10) N2,  mod(level,100) N3, mod(level,1000) N4,
mod(level,1000) N5, mod(level,10000) N6, mod(level,5) N7, mod(level,50) N8, mod(level,500) N9,
case when mod(level,10000)=0 then 'AIOUG' else dbms_random.string('A',10) end V6,
mod(level,5000) N10, mod(level,50000) N11
from dual connect by level

create table T2 as
select     level ID, mod(level,2) N1, mod(level,100) N3, mod(level,1000) N4, mod(level,1000) N5,
mod(level,10000) N6, mod(level,5) N7, mod(level,50) N8, mod(level,500) N9,
case when mod(level,10000)=0 then 'AIOUG' else dbms_random.string('A',10) end V6,
mod(level,5000) N10, mod(level,50000) N11,
mod(level,10) N2   
from dual connect by level

exec dbms_stats.gather_table_stats(user,'T1');
exec dbms_stats.gather_table_stats(user,'T2');

--注意N2定义的顺序不一样.

SCOTT@test> select owner, num_rows, blocks, last_analyzed from dba_tables where table_name in ('T1','T2') and owner=user;
OWNER    NUM_ROWS     BLOCKS LAST_ANALYZED
------ ---------- ---------- -------------------
SCOTT      100000        846 2015-05-08 09:36:05
SCOTT      100000        846 2015-05-08 09:36:02

$ cat x1.sql
set termout off
select id,&2 from &1;
set termout on
--避免不必要的输出.
SCOTT@test> alter session set statistics_level=all;
Session altered.

SCOTT@test> @x1 t1 n1
SCOTT@test> @dpc '' ''
PLAN_TABLE_OUTPUT
-------------------------------------
SQL_ID  3svh6k0ryh0uv, child number 0
-------------------------------------
select id,n1 from t1
Plan hash value: 3617692013
--------------------------------------------------------------------------------------------------------------------
| Id  | Operation         | Name | Starts | E-Rows |E-Bytes| Cost (%CPU)| E-Time   | A-Rows |   A-Time   | Buffers |
--------------------------------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT  |      |      1 |        |       |   149 (100)|          |    100K|00:00:00.11 |    1319 |
|   1 |  TABLE ACCESS FULL| T1   |      1 |    100K|   781K|   149   (1)| 00:00:01 |    100K|00:00:00.11 |    1319 |
--------------------------------------------------------------------------------------------------------------------
Query Block Name / Object Alias (identified by operation id):
-------------------------------------------------------------
   1 - SEL$1 / T1@SEL$1

SCOTT@test> @x1 t2 n1
SCOTT@test> @dpc '' ''
PLAN_TABLE_OUTPUT
-------------------------------------
SQL_ID  6yd1cud8ck97y, child number 0
-------------------------------------
select id,n1 from t2
Plan hash value: 1513984157
--------------------------------------------------------------------------------------------------------------------
| Id  | Operation         | Name | Starts | E-Rows |E-Bytes| Cost (%CPU)| E-Time   | A-Rows |   A-Time   | Buffers |
--------------------------------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT  |      |      1 |        |       |   149 (100)|          |    100K|00:00:00.11 |    1319 |
|   1 |  TABLE ACCESS FULL| T2   |      1 |    100K|   781K|   149   (1)| 00:00:01 |    100K|00:00:00.11 |    1319 |
--------------------------------------------------------------------------------------------------------------------
Query Block Name / Object Alias (identified by operation id):
-------------------------------------------------------------
   1 - SEL$1 / T2@SEL$1

--可以发现两种CPU cost一样.buffers也一样.

--查询N2字段看看:

SCOTT@test> @x1 t1 n2
SCOTT@test> @dpc '' ''
PLAN_TABLE_OUTPUT
-------------------------------------
SQL_ID  4ssczwvuk346y, child number 0
-------------------------------------
select id,n2 from t1
Plan hash value: 3617692013
--------------------------------------------------------------------------------------------------------------------
| Id  | Operation         | Name | Starts | E-Rows |E-Bytes| Cost (%CPU)| E-Time   | A-Rows |   A-Time   | Buffers |
--------------------------------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT  |      |      1 |        |       |   149 (100)|          |    100K|00:00:00.11 |    1319 |
|   1 |  TABLE ACCESS FULL| T1   |      1 |    100K|   781K|   149   (1)| 00:00:01 |    100K|00:00:00.11 |    1319 |
--------------------------------------------------------------------------------------------------------------------

Query Block Name / Object Alias (identified by operation id):
-------------------------------------------------------------

   1 - SEL$1 / T1@SEL$1

SCOTT@test> @dpc '' ''
PLAN_TABLE_OUTPUT
-------------------------------------
SQL_ID  8aau83a5ct1u9, child number 0
-------------------------------------
select id,n2 from t2
Plan hash value: 1513984157
--------------------------------------------------------------------------------------------------------------------
| Id  | Operation         | Name | Starts | E-Rows |E-Bytes| Cost (%CPU)| E-Time   | A-Rows |   A-Time   | Buffers |
--------------------------------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT  |      |      1 |        |       |   150 (100)|          |    100K|00:00:00.11 |    1319 |
|   1 |  TABLE ACCESS FULL| T2   |      1 |    100K|   781K|   150   (2)| 00:00:01 |    100K|00:00:00.11 |    1319 |
--------------------------------------------------------------------------------------------------------------------
Query Block Name / Object Alias (identified by operation id):
-------------------------------------------------------------
   1 - SEL$1 / T2@SEL$1

--可以发现两种CPU cost查询T2时稍微多1个.buffers也一样.说明列的顺序对cpu cost存在一定的影响.一般设置是把经常查询列放前面.

--补充链接里面是如何计算的.

SCOTT@test> explain plan for select id,n1 from t1;
Explained.

SCOTT@test> column options format a30
SCOTT@test> select OPERATION, OPTIONS, COST, CPU_COST, IO_COST, TIME from plan_table;
OPERATION         OPTIONS   COST   CPU_COST    IO_COST       TIME
----------------- -------- ----- ---------- ---------- ----------
SELECT STATEMENT             149   23024738        148          1
TABLE ACCESS      FULL       149   23024738        148          1

--退出,我的plan_table 是临时表.
SCOTT@test> explain plan for select id,n2 from t2;
Explained.

SCOTT@test> select OPERATION, OPTIONS, COST, CPU_COST, IO_COST, TIME from plan_table;
OPERATION         OPTIONS  COST   CPU_COST    IO_COST       TIME
----------------- -------- ---- ---------- ---------- ----------
SELECT STATEMENT            150   45024738        148          1
TABLE ACCESS      FULL      150   45024738        148          1

The CPU_Cost for a Full Table Scan of T1 is 23024738. I assume, the other numbers are Oracle calculations and are the
defaults. 7121.44 Cpu Cycles per block multiplied by number of blocks. 150 CPU Cycles for each Row and 20 CPU Cycles for
Column Skip. So, 20*100000*(2-1) is for 20 CPU Cycles multiplied by Highest ID – Lowest ID (2-1) of the columns
referred in the query. Since the two tables are same in terms of number of rows and blocks, the calculation for a FTS
should remain same. Lets see..

--这些来自哪里?

SCOTT@test> column pname format a20
SCOTT@test> Select * from sys.aux_stats$;
SNAME                          PNAME                     PVAL1 PVAL2
------------------------------ -------------------- ---------- --------------------
SYSSTATS_INFO                  STATUS                          COMPLETED
SYSSTATS_INFO                  DSTART                          03-02-2015 11:52
SYSSTATS_INFO                  DSTOP                           03-02-2015 11:52
SYSSTATS_INFO                  FLAGS                         0
SYSSTATS_MAIN                  CPUSPEEDNW                 1517
SYSSTATS_MAIN                  IOSEEKTIM                    10
SYSSTATS_MAIN                  IOTFRSPEED                 4096
SYSSTATS_MAIN                  SREADTIM
SYSSTATS_MAIN                  MREADTIM
SYSSTATS_MAIN                  CPUSPEED
SYSSTATS_MAIN                  MBRC
SYSSTATS_MAIN                  MAXTHR
SYSSTATS_MAIN                  SLAVETHR

13 rows selected.

SCOTT@test> select round(7121.44*&blocks+(150*100000)+(20*100000*(2-1))) from dual;
Enter value for blocks: 846
ROUND(7121.44*846+(150*100000)+(20*100000*(2-1)))
-------------------------------------------------
                                         23024738

SCOTT@test> select round(7121.44*&blocks+(150*100000)+(20*100000*(13-1))) from dual;
Enter value for blocks: 846
ROUND(7121.44*846+(150*100000)+(20*100000*(13-1)))
--------------------------------------------------
                                          45024738

--居然都对上了,这些信息.如果查询这样按照作者的介绍CPU cost会更多.

SCOTT@test> explain plan for select id, n1 from t2 where n2=:b1;
Explained.

SCOTT@test> column options format a30
SCOTT@test> select OPERATION, OPTIONS, COST, CPU_COST, IO_COST, TIME from plan_table;
OPERATION         OPTIONS  COST   CPU_COST    IO_COST       TIME
----------------- -------- ---- ---------- ---------- ----------
SELECT STATEMENT            151   60024738        148          1
TABLE ACCESS      FULL      151   60024738        148          1

--要在多记一次150 CPU Cycles for each Row.不理解???明白好像在where条件还有计算一次.

SCOTT@test> select 23024738+150*100000+(20*100000*(13-2)) from dual;
23024738+150*100000+(20*100000*(13-2))
--------------------------------------
                              60024738

--对照下面的执行就很清楚了:
SCOTT@test> explain plan for select id, n1 ,n2 from t2 ;
Explained.

SCOTT@test> column options format a30
SCOTT@test> select OPERATION, OPTIONS, COST, CPU_COST, IO_COST, TIME from plan_table;

OPERATION         OPTIONS  COST   CPU_COST    IO_COST       TIME
----------------- -------- ---- ---------- ---------- ----------
SELECT STATEMENT            150   45024738        148          1
TABLE ACCESS      FULL      150   45024738        148          1

时间: 2024-09-08 20:31:16

[20150508]列顺序问题.txt的相关文章

如何更改表中的Mysql列顺序

首先,请考虑是否的确需要更改表中的列顺序.SQL的核心要点是从数据存储格式获取应用.总应指定检索数据的顺序.在下面的第1条语句中,以col_name1.col_name2.col_name3顺序返回列:在第2条语句中,以col_name1.col_name3.col_name2顺序返回列: MySQL> SELECT col_name1, col_name2, col_name3 FROM tbl_name; mysql> SELECT col_name1, col_name3, col_na

SqlServer(索引)--创建复合索引时,复合索引列顺序对查询的性能影响[转]

http://www.cnblogs.com/wy123/p/5604400.html SQL Server创建复合索引时,复合索引列顺序对查询的性能影响 说说复合索引 写索引的博客太多了,一直不想动手写,有一下两个原因: 一是觉得有炒剩饭的嫌疑,有兄弟曾说:索引吗,只要在查询条件上建索引就行了,真的可以这么暴力吗? 二来觉得,索引是个非常大的话题,很难概括出所有的情况,你不整出点新意来,倒是有抄袭照搬的嫌疑 既然写了,就写一点稍微不一样的东西出来, 好了,废话打住, /* 20160814备注

Oracle列顺序的影响

 Oracle列顺序的影响  通常情况下,dba或开发人员在进行表设计的时候,都不会考虑到列在表中的顺序的问题,那么列的顺序对我们加快访问表中的数据有没有作用喃?如果我们知道了数据库引擎是怎样在块中存储行的话,这个问题我们就能容易明白了. H:header记录了行本身的一些属性信息,比如是否锁定,该行中有多少列等: Ln:length每列的长度 Dn:data 数据 create table test_column_order ( id1 number, id2 number, id3 numb

急用 万分感谢-C++顺序读取txt的问题

问题描述 C++顺序读取txt的问题 0,-1145.1,2037.8,-0,-494.02,0 0,-2285.2,4060.2,-0,-982.1,0 0,-3415.9,6063,-0,-1464.1,0 0,-4541.8,8048.5,-0,-1939,0 0,-5659.5,10018,-0,-2409.6,0 我想顺序的读取这样的数据,然后把每一行数据顺序赋给x y z a b c,然互按照行的顺序不断刷新 x y z a b c,请大神指导,我有个思路是用两个循环,但不知有什么好

SQL Server创建复合索引时,复合索引列顺序对查询的性能影响

原文:SQL Server创建复合索引时,复合索引列顺序对查询的性能影响    说说复合索引 写索引的博客太多了,一直不想动手写,有一下两个原因: 一是觉得有炒剩饭的嫌疑,有兄弟曾说:索引吗,只要在查询条件上建索引就行了,真的可以这么暴力吗? 二来觉得,索引是个非常大的话题,很难概括出所有的情况,你不整出点新意来,倒是有抄袭照搬的嫌疑 既然写了,就写一点稍微不一样的东西出来, 好了,废话打住,开搞   搭建测试环境: 创建一张表,模拟实际业务中的一个表,往里面填入数据, 时间字段上,相对按照时间

如何能使GridView的列能拖动,并且在拖动后,页面再次刷新后,列顺序还是拖动后的顺序

问题描述 如何能使GridView的列能拖动,并且在拖动后,页面再次刷新后,列顺序还是拖动后的顺序 解决方案 解决方案二:asp做这个似乎难实现,帮顶,学习.解决方案三:第三方..DWZORjqgrid.

[20160330]关于连接顺序3.txt

[20160330]关于连接顺序3.txt --关于连接顺序,曾经写过两篇blog,链接如下: http://blog.itpub.net/267265/viewspace-1991306/ http://blog.itpub.net/267265/viewspace-1991787/ --今天才发现自己犯了一个严重错误,使用外连接的情况,连接顺序是可以改变的,以前的blog存在严重错误-(:) --还是通过例子来讲解: 1.环境: SCOTT@book> @ &r/ver1 PORT_ST

[20160725]字段超过255列的问题.txt

[20160725]字段超过255列的问题.txt --曾经写过1篇blog, 如果字段很多超过255列,oracle选择多个行片保存. [20121025]1条记录会有多少row pieces.txt => http://blog.itpub.net/267265/viewspace-747213/ --我以前的测试非常特殊,导致大量的行迁移.以前测试忽略的问题,自己重复看看: 1.测试: spool a.sql select 'create table t1 (' from dual uni

[20161021]显示记录顺序问题.txt

[20161021]显示记录顺序问题.txt --同事在维护数据库时,发现记录显示顺序发生变化,看了一下操作过程,可以猜测可能维护后发生了行迁移导致的情况. --通过例子说明: 1.建立测试环境: SCOTT@book> @ &r/ver1 PORT_STRING         VERSION    BANNER ------------------- ---------- ----------------------------------------------------------