[tds_menu_login inline="yes" guest_tdicon="td-icon-profile" logout_tdicon="td-icon-log-out" show_menu="yes" toggle_txt="Your Profile" menu_uh_padd="12px 20px" menu_ul_padd="12px 20px" menu_ulo_padd="12px 20px" menu_gc_padd="12px 20px" menu_gh_padd="15px 20px" menu_uh_color="var(--lc-primary)" menu_uh_border_color="var(--lc-lgrey-c1)" menu_bg="var(--lc-greyish)" menu_ul_link_color="var(--lc-black)" menu_ul_link_color_h="var(--lc-primary)" menu_ul_sep_color="var(--lc-lgrey-c1)" menu_uf_txt_color="var(--lc-black)" menu_uf_txt_color_h="var(--lc-primary)" menu_uf_icon_color="var(--lc-black)" menu_uf_icon_color_h="var(--lc-primary)" menu_uf_border_color="var(--lc-lgrey-c1)" f_uh_font_family="sans-serif_global" f_links_font_family="sans-serif_global" f_uf_font_family="sans-serif_global" f_uh_font_size="14" f_links_font_size="14" f_uf_font_size="14" f_uf_font_line_height="1.2" f_links_font_line_height="1.2" f_uh_font_line_height="1.2" f_uh_font_weight="500" f_links_font_weight="400" f_uf_font_weight="500" menu_ul_sep_space="0" menu_ul_space="10" f_gh_font_family="sans-serif_global" f_btn1_font_family="sans-serif_global" f_btn2_font_family="sans-serif_global" show_version="" toggle_txt_color="var(--lc-black)" toggle_txt_color_h="var(--lc-primary)" icon_color="var(--lc-black)" icon_color_h="var(--lc-primary)" f_toggle_font_family="sans-serif_global" f_toggle_font_transform="" f_toggle_font_size="eyJhbGwiOiIxNiIsInBvcnRyYWl0IjoiMTQifQ==" f_toggle_font_line_height="1.2" f_toggle_font_weight="600" menu_arrow_color="var(--lc-greyish)" menu_border_color="var(--lc-greyish)" menu_shadow_shadow_size="0" menu_shadow_shadow_offset_vertical="0" f_gh_font_size="14" f_btn1_font_size="14" f_btn2_font_size="14" f_btn2_font_line_height="1.2" f_btn1_font_line_height="1.2" f_gh_font_line_height="1.2" menu_gh_color="var(--lc-primary)" menu_gh_border_color="var(--lc-lgrey-c1)" menu_gc_btn1_bg_color="var(--lc-lgrey-c1)" menu_gc_btn1_bg_color_h="var(--lc-primary)" menu_gc_btn1_color="var(--lc-black)" menu_gc_btn1_color_h="var(--lc-white)" menu_gc_btn1_border_color="var(--lc-lgrey-c1)" menu_gc_btn1_border_color_h="var(--lc-primary)" menu_gc_btn2_color="var(--lc-black)" menu_gc_btn2_color_h="var(--lc-primary)" f_gh_font_weight="500" f_btn2_font_weight="500" f_btn1_font_weight="500" menu_gh_txt="Explore account options." icon_size="20" avatar_size="20" disable_dropdown="yes" tdc_css="eyJhbGwiOnsibWFyZ2luLWJvdHRvbSI6IjAiLCJwYWRkaW5nLXJpZ2h0IjoiMjAiLCJib3JkZXItY29sb3IiOiJ2YXIoLS1sYy1kYXJrLWJsdWUpIiwiZGlzcGxheSI6IiJ9fQ=="]

Become a member

Get the latest updates and tutorials relating to Artificial Intelligence (AI).

― Advertisement ―

How to Write Engaging Blogs People Want to Read

Tips and tricks to write engaging blogs. From understanding your audience to SEO optimization, this guide covers it all.
HomeArchiveTemporary Tables With MySQL

Temporary Tables With MySQL

If you have had problems with MySQL not supporting functionality like sub selects this short article might be of interest for you. This article will explain how to use temporary tables with MySQL.

The juice

The SQL code below shows you how you can create a temporary table.

CREATE TEMPORARY TABLE TempTable ( ID int, Name char(100) ) TYPE=HEAP;
INSERT INTO TempTable VALUES( 1, “Foo bar” );
SELECT * FROM TempTable;

If you’re using an MySQL version older than 3.23, you should use the code below.

CREATE TABLE TempTable ( ID int, Name char(100) ) TYPE=HEAP;
INSERT INTO TempTable VALUES( 1, “Foo bar” );
SELECT * FROM TempTable;

Temporary tables

Temporary tables are per connection so heavy loaded sites will not have a problem with using the same table name since it’s unique per connection. If there already exists a table, which is not temporary, with the same same this table is then hidden until the temporary table is deleted. Temporary tables where added in MySQL version 3.23.

If you use an older version of MySQL than 3.23 you can’t use temporary tables, but you can use heap tables.

If the connection to the database is lost the temporary table is automatically deleted.

Heap tables

The heap tables are shared between all connections. Therefore you must use locking of the temporary table to ensure that no other connection is corrupting the data in your temporary table.

Since we need speed and the tables are temporary we create them as heap tables. This means that the tables are stored in memory and not on disk. Of course the data will be lost if the power is lost, but the kind of information stored in a heap table should be temporary data used for further queries.

To free memory always remember to drop the heap tables when you don’t need them anymore. Or at least delete the rows which is no longer of interest. Heap tables can take up a lot of memory if you don’t clean up after you’re done.

Heap tables are shared between all clients (just like any other table), so use them with care.

Using the LOCK TABLE mechanism

Since lock table works across connections this can be used to mimic the behaviour of the temporary tables. Although this is not that effective on servers with many connections, it works.

The lock table mechanism ensures that no other connections get access to write to the table so you don’t get corrupted data in the table while you are using it.

If the connection to the database is lost the locks are automatically unlocked.

Zez is a community website for developers, graphics and content designers. We aim to satisfy the intermediate to expert developers. For more information please visit http://www.zez.org


Please enter your comment!
Please enter your name here