Usage of temporary tables in MySQL databases

Featured Image

Hi! My recent project had some specific requirements regarding the work with database. Since DB server had some limitations (regarding number of queries), and fairly big number of queries that needed to be executed I created a solution that included temp tables. And now, I’d like to explain how to use them and why.

Suppose that in your application, you need to execute a calculation on a set of tables. The usual approach is to create a query that defines the joins between multiple tables, and selects the data from the joined tables. The problem with that approach is that every time you call that the query, the tables have to be rejoined in order to create the result. And that is usually fairly resource hungry. Instead, you can get around the problem by putting the results into temporary table, so the values are there while the database connection lasts.

And to conclude this short introduction, you will see the full benefit of this approach only if you have (over)complicated query to execute. If you are writing a simple CRUD (Create Read Update Delete) application, there’s no need for temp tables.

An example of reasonable usage would be batch import of data into Magento EAV system, which is fairly complicated. A fast way of import itself would be to save import file (CSV) into a temporary table, and work with different “SELECT” queries to populate the EAV.

So, here’s how it’s created:

CREATE TEMPORARY TABLE tmp_tbl_name (
 
id INT,
 
VALUE VARCHAR
 
) ENGINE MyISAM;

And if you have other table structured like:

CREATE TABLE tbl_name (
 
id INT,
 
name VARCHAR,
 
description VARCHAR,
 
VALUE VARCHAR,
 
) ENGINE MyISAM;

But you just wish to use value from that table for some reason you can just write:

INSERT INTO  tmp_tbl_name
 
SELECT VALUE FROM   tbl_name
 
WHERE id > 1;

The intentionally put a “WHERE” clause in this last SQL snippet. That’s because I wanted to show you that you can use standard “SELECT” for inserting the data. Also, you virtually have no limitations on that part because you can use multiple joins on multiple temp/regular tables etc.

I’ll look into writing a bit more complex example in near future. I hope you learned something!

Until next time, bye.


2 comments

  1. I have an error message as following.
    Please help me.
    Thank you

    exception ‘Zend_Db_Statement_Exception’ with message ‘SQLSTATE[42S22]: Column not found: 1054 Unknown column ‘created_at’ in ‘where clause” in
    /…/html/lib/Zend/Db/Statement/Pdo.php:234 Stack trace: #0 /…/lib/Zend/Db/Statement.php(300): Zend_Db_Statement_Pdo->_execute(Array) #1
    /…/lib/Zend/Db/Adapter/Abstract.php(468): Zend_Db_Statement->execute(Array) #2
    /…/lib/Zend/Db/Adapter/Pdo/Abstract.php(238): Zend_Db_Adapter_Abstract->query(‘SELECT `main_ta…’, Array) #3 /…/lib/Varien/Db/Adapter/Pdo/Mysql.php(333): Zend_Db_Adapter_Pdo_Abstract->query(‘SELECT `main_ta…’, Array) #4 /…l/lib/Zend/Db/Adapter/Abstract.php(706): Varien_Db_Adapter_Pdo_Mysql->query(Object(Varien_Db_Select), Array) #5 /…/lib/Varien/Data/Collection/Db.php(707): Zend_Db_Adapter_Abstract->fetchAll(Object(Varien_Db_Select), Array) #6 /…/lib/Varien/Data/Collection/Db.php(620): Varien_Data_Collection_Db->_fetchAll(Object(Varien_Db_Select)) #7 /…/lib/Varien/Data/Collection/Db.php(590): Varien_Data_Collection_Db->getData() #8
    /…/lib/Varien/Data/Collection.php(264): Varien_Data_Collection_Db->load() #9
    /…/app/code/core/Mage/Ideal/Model/Advanced.php(189): Varien_Data_Collection->getItems() #10 [internal function]: Mage_Ideal_Model_Advanced->transactionStatusCheck(Object(Mage_Cron_Model_Schedule)) #11 /…/app/code/core/Mage/Cron/Model/Observer.php(105): call_user_func_array(Array, Array) #12 /…/app/code/core/Mage/Core/Model/App.php(1228): Mage_Cron_Model_Observer->dispatch(Object(Varien_Event_Observer)) #13 /…/app/code/core/Mage/Core/Model/App.php(1209): Mage_Core_Model_App->_callObserverMethod(Object(Mage_Cron_Model_Observer), ‘dispatch’, Object(Varien_Event_Observer)) #14
    /…/app/Mage.php(416): Mage_Core_Model_App->dispatchEvent(‘default’, Array) #15
    /…/cron.php(44): Mage::dispatchEvent(‘default’) #16 {main}

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <blockquote cite=""> <code> <del datetime=""> <em> <s> <strike> <strong>. You may use following syntax for source code: <pre><code>$current = "Inchoo";</code></pre>.