Reputation: 30093
So my boss gave me a stored procedure that needs to be optimized since it is slow. The procedure is creating a temporary table in the process and it looks weird:
/* pseudocode for brevity */
create temp_table;
insert into temp_table (...)
select ...
from other_table
inner join ...
inner join ...
where condition
select * from temp_table
So that is it. The code inserts data into the temporary table only to be retrieved right after. Here's what I did, I removed the temporary table and return directly the selected records:
/* pseudocode for brevity */
select ...
from other_table
inner join ...
inner join ...
where condition
The code before looks wrong, but I want to be sure what a temporary table does. So I read about temporary tables and figured that temporary tables lasts and can only be accessed in a single session. My question:
Upvotes: 0
Views: 501
Reputation: 351
As for SQL connection, Session means one connection u create. Once you make a connection to your database,the specified session will be created by database server.
And as an admitted newbie dev on SQL, I always choose temporary table when I need to do a branch of queries through many tables.(PS: my experience shows me that those rows stored in temporary table will no be updated though their value have been changed)
Similar question here.
Upvotes: 0
Reputation: 11
A session in Database mean a operation between opening an closing of a connection.
Eg. In a application side if i try to call a stored procedure and before that i open a connection to DB.The scope of a temporary table available until i say connection closed
Upvotes: 1