I have a MySQL stored procedure that uses a temporary table. Assume that my table name is 'temp' and I use it to store some middle data. It will create at the beginning of procedure, and will drop at the end.
CREATE PROCEDURE p() BEGIN CREATE TEMPORARY TABLE \`temp\`(...); INSERT INTO \`temp\` VALUES(...); DROP TEMPORARY TABLE \`temp\`; END;
The problem is that this stored procedure may be used by different users concurrently, so I want to know if this can cause any problems (i.e. any conflict in inserted data in temp table). In other word is temp table a shared resource within different calls to the same SP?
According to my knowledge if a temporary table is called from different processes they will only be shared if they are defined using a double hash ##Table. Otherwise they are only visible to the current process and processes spawned by that process.
In MySQL, a temporary table is a special type of table that allows you to store a temporary result set, which you can reuse several times in a single session. A temporary table is very handy when it is impossible or expensive to query data that requires a single SELECT statement with the JOIN clauses.
An internal temporary table can be held in memory and processed by the MEMORY storage engine, or stored on disk by the InnoDB or MyISAM storage engine. If an internal temporary table is created as an in-memory table but becomes too large, MySQL automatically converts it to an on-disk table.
Views are stored queries for existing data in existing tables. Temporary table needs to be populated first with data, and population is the main preformance-concerned issue. So the data in views already exists and so views are faster than temporary table.
No, a temp table is limited to the scope of your database connection. You can use the temp table in subsequent calls to the procedure during the same database connection, but other connections cannot access it. They can create a table by the same name, but each temp table will be independent. Temp tables go away when you close your connection.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With