天天看點

關于庫表散列

在mysql庫表散列中總結的SQL語句(ZT) 2008年01月16日 星期三 09:11 mysql> select goodsId,goodsName from goods;

+----------------+--------------+

| goodsId        | goodsName    |

+----------------+--------------+

| 0B0ADABA002002 | 鍗у 绯誨垪 |

| 0B0ADABA001001 | 瀹㈠巺绯誨垪 |

| 0B0ADABA002003 | 鍗у 绯誨垪 |

| 0B0ADABA001002 | 瀹㈠巺绯誨垪 |

| 0B0ADABA002004 | 椁愬巺绯誨垪 |

| 0B0ADABA001003 | 鍗у 绯誨垪 |

| 0B0ADABA002005 | 鍗у 绯誨垪 |

| 0B0ADABA001004 | 鍗у 绯誨垪 |

| 0B0ADABA003001 | 瀹㈠巺绯誨垪 |

| 0B0ADABA002001 | 涔︽埧绯誨垪 |

+----------------+--------------+

10 rows in set (0.00 sec)

mysql> select goodsId,goodsName from goods where goodsId like '%002%';

+----------------+--------------+

| goodsId        | goodsName    |

+----------------+--------------+

| 0B0ADABA002002 | 鍗у 绯誨垪 |

| 0B0ADABA002003 | 鍗у 绯誨垪 |

| 0B0ADABA001002 | 瀹㈠巺绯誨垪 |

| 0B0ADABA002004 | 椁愬巺绯誨垪 |

| 0B0ADABA002005 | 鍗у 绯誨垪 |

| 0B0ADABA002001 | 涔︽埧绯誨垪 |

+----------------+--------------+

6 rows in set (0.00 sec)

mysql> select goodsId,goodsName from goods where goodsId like '________002%';

+----------------+--------------+

| goodsId        | goodsName    |

+----------------+--------------+

| 0B0ADABA002002 | 鍗у 绯誨垪 |

| 0B0ADABA002003 | 鍗у 绯誨垪 |

| 0B0ADABA002004 | 椁愬巺绯誨垪 |

| 0B0ADABA002005 | 鍗у 绯誨垪 |

| 0B0ADABA002001 | 涔︽埧绯誨垪 |

+----------------+--------------+

5 rows in set (0.00 sec)

mysql> select ASCII(goodsId) from goods;

+----------------+

| ASCII(goodsId) |

+----------------+

|             48 |

|             48 |

|             48 |

|             48 |

|             48 |

|             48 |

|             48 |

|             48 |

|             48 |

|             48 |

+----------------+

10 rows in set (0.01 sec)

mysql> SELECT CONCAT(2,'test');

+------------------+

| CONCAT(2,'test') |

+------------------+

| 2test            |

+------------------+

1 row in set (0.00 sec)

mysql> SELECT CAST(38.8 AS CHAR);

+--------------------+

| CAST(38.8 AS CHAR) |

+--------------------+

| 38.8               |

+--------------------+

1 row in set (0.00 sec)

mysql> select SUBSTRING(goodsId,4,2) from goods;

+------------------------+

| SUBSTRING(goodsId,4,2) |

+------------------------+

| AD                     |

| AD                     |

| AD                     |

| AD                     |

| AD                     |

| AD                     |

| AD                     |

| AD                     |

| AD                     |

| AD                     |

+------------------------+

10 rows in set (0.00 sec)

mysql> select ASCII( SUBSTRING(goodsId,4,2)) from goods;

+--------------------------------+

| ASCII( SUBSTRING(goodsId,4,2)) |

+--------------------------------+

|                             65 |

|                             65 |

|                             65 |

|                             65 |

|                             65 |

|                             65 |

|                             65 |

|                             65 |

|                             65 |

|                             65 |

+--------------------------------+

10 rows in set (0.00 sec)

mysql> select ASCII( SUBSTRING(goodsId,4,2)),ASCII( SUBSTRING(goodsId,5,2)) from goods;

+--------------------------------+--------------------------------+

| ASCII( SUBSTRING(goodsId,4,2)) | ASCII( SUBSTRING(goodsId,5,2)) |

+--------------------------------+--------------------------------+

|                             65 |                             68 |

|                             65 |                             68 |

|                             65 |                             68 |

|                             65 |                             68 |

|                             65 |                             68 |

|                             65 |                             68 |

|                             65 |                             68 |

|                             65 |                             68 |

|                             65 |                             68 |

|                             65 |                             68 |

+--------------------------------+--------------------------------+

10 rows in set (0.00 sec)

mysql> select ASCII( SUBSTRING(goodsId,4,2))*100+ASCII( SUBSTRING(goodsId,5,2)) from goods;

+-------------------------------------------------------------------+

| ASCII( SUBSTRING(goodsId,4,2))*100+ASCII( SUBSTRING(goodsId,5,2)) |

+-------------------------------------------------------------------+

|                                                              6568 |

|                                                              6568 |

|                                                              6568 |

|                                                              6568 |

|                                                              6568 |

|                                                              6568 |

|                                                              6568 |

|                                                              6568 |

|                                                              6568 |

|                                                              6568 |

+-------------------------------------------------------------------+

10 rows in set (0.00 sec)

資料庫中的散列法是使用計算值來配置設定表格資料的方法,它比在整個索引中搜尋要好的多。一個哈希散列答應你在資料庫表格中存儲資料,以便這些行的要害計算的相同值存儲在相同的位置。

為了在哈希散列中找到一個行,查詢機應用哈希函數到一個行的要害值,然後配置設定和那個值相關的資料塊。在很多情況下,一個哈希散列比一個普通的索引快。

  Oracle在Oracle 7面世的時候就支援哈希散列。哈希散列的優勢僅僅在于當表格的通路在要害值上首先使用的是=操作符,這個表格是靜态的,并且僅僅當資料行需要的時候。當和一個普通非索引或者散清單格比較的時候,全表掃描就會變慢。

  比如,假設你想要建立一個表格來查找英語單詞的發音。你需要迅速的配置設定一個英語單詞的發音,但是一個字典,比如cmudict0.3有大概106,000個單詞。

  建立哈希散列最大的工作任務就是分析參數。你需要計算分析每個散列的帶和散列要害字包含的内容數量。假如三類是正整數,那麼你可以設定最小值的大小假如你需要定義自己哈希函數。

  對于任何其他類型的資料,比如下面這個列子,你需要計算參數的最小大小。你可以通過使用DBMS_UTILITY.GET_HASH_VALUE函數來獲得一個确切的估計。

  create table cmudict

  (

  Word varchar2(22) primary key,

  pron varchar2(62)

  );

  使用其他的快速裝入程式比如SQL*來裝入這些資料:

  select max(blksize)

  from (select sum(3+nvl(vsize(word),0)+1+nvl(vsize(pron),0)+1) blksize

  from cmudict group by dbms_utility.get_hash_value(word,1,10007)) blkqry;

   NVL(VSIZE(col),0)+1表達式使用每個欄來分析位元組的數字。3+是行的開銷,是以表達式的總和是存儲每個行所需要的大小。在每個哈希函 數組中,求和行的大小,我們獲得每個散列資料塊所許喲啊的位元組數量。1是真的不切實際,但是10007,一個質數,是對哈希要害字參數的最好猜測。

  增加或者減少這個值到另外一個質數知道上面的分析結果降低資料塊大小的重要性。這些數字,分析的結果和GET_HASH_VALUE的第三個參數應該被使用來創造散列。

  現在你可以創造一個散列并使用這個散列存儲資料再建立一個表格。

  create cluster cmudict_cluster (word varchar2(22))

  size 6000

  single table

  hashkeys 739;

  create table cmudict

  (

  word varchar2(22) not null,

  pron varchar2(62)

  )

  cluster cmudict_cluster(word);

  假如你需要分析這個散列:

  analyze cluster cmudict_cluster compute statistics;

  select * from user_clusters where cluster_name = 'CMUDICT_CLUSTER';

  你可以看到AVG_BLOCKS_PER_KEY 是 1.

  可以為查詢語句做注解select * from cmudict where word = 'HELLO' returns:

  SELECT STATEMENT

  TABLE Access HASH

  CMUDICT

//

oracle資料庫表分區

關鍵字: oracle資料庫表分區 Oracle表分區

自從oracle8i 開始可以把一個表分割為多個小的部分,這樣可以對oracle的性能優化帶來很大的好處~

例如:改善表的查詢性能,更加容易管理表資料,備份和恢複操作更友善

在oracle 中分區表 分為好幾種的(範圍分區,散列分區,子分區,清單分區,索引分區)下面我們來慢慢介紹

現在我們來建立一個[範圍分區]

create table RangeTable(

id int primary key,

name varchar(20),

grade int

)

partition by rang(grade)

(

partition part1 values less then(50) tablespace Part1_tb,

partition part2 values less then(MAXVALUE) tablespace Part2_tb

);

如果grade的值小于50的話 就把記錄放到名為part1的分區當中,part1分區将被存儲在Part1_tb表空間中

其他的就放在part2中 MAXVALUE是oracle的關鍵字 表示最大值

[散列分區]

create table HashTable(

id int primary key,

name varchar(20),

grade int

)

partition by hash(grade)

partitions 10 -- 指定分區的數目

store in(Part1_tb,Part2_tb,Part3_tb) --如果指定的分區數目比表空間多,分區會以循環方式配置設定到表空間

partition by rang(grade)--這種方式就是 指定以命名的分區

(

partition part1 tablespace Part1_tb,

partition part2 tablespace Part2_tb

);

[子分區]即是分區的分區

create table ChildTable(

id int primary key,

name varchar(20),

grade int

)

partition by rang(grade)

subpartition by hash(grade)

partitions 5

(

partition part1 values less then(30) tablespace Part1_tb,

partition part2 values less then(60) tablespace Part2_tb,

partition part3 values less then(MAXVALUE) tablespace Part3_tb

);

[清單分區]告訴oracle所有可能的值

create table ListTable(

id int primary key,

name varchar(20),

area varchar(10)

)

partition by list(area)

(

partition part1 values('guangdong','beijing') tablespace Part1_tb,

partition part2 values('shanghai','nanjing') tablespace Part2_tb

);

[索引分區]索引也可以按照和表進行分區時使用的相同的值範圍來分區

create index IndexTable_index

on IndexTable(name)

local

(

partition part1 tablespace Part1_tb,

partition part2 tablespace Part2_tb

)

--local 告訴oracle表 IndexTable的每一個分區建立一個獨立的索引

create index IndexTable_index

on IndexTable(name)

global;

--global為全局索引 全局索引可以包含多個分區的值 局部索引比全局索引容易管理,而全局索引比較快

注意:不能為散列分區 或者 子分區建立全局索引

查詢某一個表分區

select * from table partition(part1);