mysql 暫存資料表和記憶體表建立 查詢 刪除以及注意事項

來源:互聯網
上載者:User

mysql教程 暫存資料表和記憶體表建立 查詢 刪除以及注意事項
暫存資料表和記憶體表的engine 不同,暫存資料表預設的是myisam,而記憶體表是memory ,暫存資料表只對當前會話可見,串連斷開時,自動刪除!
你不必擔心所建立的暫存資料表的名稱會和其他會話建立的暫存資料表、或非暫存資料表衝突!注意如果你的暫存資料表和正常表名稱相同,正常表會被隱藏——如同全域變數和局部變數那樣
建立暫存資料表不會引發通常的commit事務提交


暫存資料表

create temporary table tmp_table

建立一個暫存資料表

create temporary table tmp_table (   name varchar(10) not null,   value integer not null   )

向暫存資料表入資料

create temporary table tmp_table select * from table_name


 
暫存資料表將在你串連mysql期間存在。當你斷開時,mysql將自動刪除表並釋放所用的空間。當然你可以在仍然串連的時候刪除表並釋放空間

也可以利用語句刪除

drop table tmp_table


引擎類型只能是:memory(heap)、myisam、merge、innodb
不支援mysql cluster
同一個查詢語句中只能引用一次! 如 select * from tp_table , tp_table as alias_name;  是錯誤的

同一個使用者儲存函數中只能引用一次!
show tables 不會顯示暫存資料表
不能使用rename重新命名暫存資料表。只能使用alter table old_tp_table_name rename new_tp_table_name;
影響使用replication功能


執行個體

記憶體表

mysql> create table tmp2(id int not null) type=heap;

執行個體

create temporary table tmp_table (   name varchar(10) not null,   value integer not null   ) type = heap


那麼速度方面呢(即myisam和memory之間的區別)?
實驗開始:
實現手段:對基於2張千萬層級的表做一些olap切分操作,中間表的建立使用2種不同的方式。最後把中間表的資料按照要求取出,插入到結果表中
實驗目的;測試臨時記憶體表和暫存資料表的速度
1.中間表的建立使用create temporary table type = heap 即 把中間表建立成臨時記憶體表
2.中間表直接使用create temporary table建立

實驗結果:
臨時記憶體表: 1小時
1 2008-09-25 11:03:48
1 2008-09-25 12:03:39
暫存資料表:1小時17分鐘
2 2008-09-25 12:25:28
2 2008-09-25 13:42:37

由此發現memory比myisam快大概20%

mysql伺服器會自動建立內部暫存資料表:該暫存資料表可以是只存在於記憶體的memory暫存資料表,或者是儲存於硬碟的myisam暫存資料表;而且 初始建立的memory暫存資料表由於表的增大 可能會轉變為myisam暫存資料表——其轉化臨界點由max_heap_table_size 和tmp_table_size系統變數的 較小值 決定的

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.