https://stackoverflow.com/questions/2825342/why-would-using-a-temp-table-be-faster-than-a-nested-query
https://www.quora.com/What-are-the-pros-and-cons-of-using-temporary-tables-vs-permanent-tables-in-MySQL
https://www.sqlshack.com/how-to-drop-temp-tables-in-sql-server/
Temporary tables are excellent for gathering results in complex searches that involve multiple queries. Our current app involves searches that are too complex to efficiently code in a single query, so we use "iterative" temporary tables to store intermediate results.
They're also good for "staging tables" where you need to store data that is to be used in another query. Here's a "staging table" use case:
Greg Kemnitz's answer to What is an efficient way to get a SQL database table to act as a unique map?
MySQL tmp tables have several advantages for this type of use: they're private to your database connection, so you don't have to worry about the table name being used by multiple db connections and causing name collisions, and they automatically "go away" once your connection goes away (although it's still good practice to DROP TEMPORARY TABLE when you're done with them, particularly if you use IN MEMORY tables, which use server-side RAM).
There are some situations where you'd use a permanent table even if it's a "scratch" table with transient data in it. If you intend for the data in the transient table to be accessible by multiple connections or apps, or if you have crash recovery requirements that involve data in the scratch table, you'll need the table to be a "permanent" table, even if the data in the table isn't permanent.
Sample for temp table :
Temp table gets dropped automiticaly when current SQL connection ends. In general, 1 request opens 1 SQL connection, hence when request ends, temp table will get dropped.
But it is also a good pratice to manually drop table
Temporary table advantages
Temporary tables behave just like normal ones; you can sort, filter, and join them as if they were permanent tables.
you're the only person who can see or use the temporary table you've created), they execute more quickly.
You can create a temporary table and insert, delete and update its records without worrying about whether you have sufficient rights to change data in permanent tables, or whether you might be accidentally doing so.
Disadvantages of a temporary table
Not as fast as variables.
Can not update in functions
Permanent table advantages
Keep data for forever.
can update in function
No comments:
Post a Comment