Dropping Temp Table At End Of Stored Procedure In MySQL - mysql

Do I need to add DROP TEMPORARY TABLE IF EXISTS data; at the end of the stored procedure even though I have the check at the top? Is there a performance implication?
CREATE DEFINER=`TEST`#`%` PROCEDURE `TEST`() BEGIN
DROP TEMPORARY TABLE IF EXISTS data;
CREATE TEMPORARY TABLE data AS
...
END;

In MySQL, temporary tables are dropped automatically when a database connection is closed. If you plan on leaving your connection open after the stored procedure, your temp table will exist on disk until that connection is closed. The performance implications depend on many factors, such as how you have configured temporary table storage on your server, how much data is in the table, etc.
It is considered best practice to just drop the temp table as soon as you are done with it. Then you save yourself the worry about those potential performance implications all together

Related

Automatically drop a temporary table when the transaction is over

I need to create a temporary table and perform a few SELECTs on it. After that I won't need that temp table. But since I'm using a connection pool, the temp table will stay there and will even interfere with the next transaction that happens to acquire the same connection.
My question is, is there a way to automatically clean-up the table when my current transaction is over?
Obviously, I could do that manually, but in order to do it properly, the code won't be that simple:
<START TX>
DROP TEMPORARY TABLE IF EXISTS some_tt;
CREATE TEMPORARY TABLE some_tt AS ...;
...
<use some_tt here>
...
DROP TEMPORARY TABLE IF EXISTS some_tt;
<END TX>
In order to release resources, we should drop the table as soon as we don't need it anymore (before the tx ends). But since we cannot guarantee the drop will happen & succeed, I believe we should also execute a drop right before the CREATE TEMPORARY TABLE statement, just in case.
A TEMPORARY table is visible only within the current session, and is dropped automatically when the session is closed. However, temporary tables are not affected by transactions and are not deleted when a transaction ends or rollbacks. In addition, the temporary table is visible to parallel transactions within the session.

Call stored procedure from stored procedure in MySQL

i'm new to MySQL.
I need to call stored procedure from a stored procedure and use the first stored procedure as a table.
How to do this without use temporary table?
How to do this without use temporary table?
create a fact table then
Not sure why there is a requirement saying: can't use temporary table as you are using store procedure. but that must be unreasonable.
if the RDBS takes care of that for you , the underlying mechanism is still store the 1st result set somewhere in the memory as middle step result. temporary table for you.
so just: create a temporary table, call the store procedure, insert data into that . why not

Alternative for a MySQL temporary table in Oracle

I noticed that the concept of temporary tables in these two systems is different, and I have a musing.. I have the following scenario in MySQL:
Drop temporary table 'a' if exists
Create temporary table 'a'
Populate it with data through a stored procedure
Use the data in another stored procedure
How can I implement the same scenario in Oracle? Can I (in one procedure preferable) create a temporary table, populate it, and insert data in another (non-temporary) table?
I think that I can use a (global) temporary table which truncates on commit, and avoid steps 1&2, but I need someone else's opinion too.
In Oracle, you very rarely need a temporary table in the first place. You commonly need temporary tables in other databases because those databases do not implement multi-version read consistency and there is the potential that someone reading data from the table would be blocked while your procedure runs or that your procedure would do a dirty read if it didn't save off the data to a separate structure. You don't need global temporary tables in Oracle for either of these reasons because readers don't block writers and dirty reads are not possible.
If you just need a temporary place to store data while you perform PL/SQL computations, PL/SQL collections are more commonly used than temporary tables in Oracle. This way, you're not pushing data back and forth from the PL/SQL engine to the SQL engine and back to the PL/SQL engine.
CREATE PROCEDURE do_some_processing
AS
TYPE emp_collection_typ IS TABLE OF emp%rowtype;
l_emps emp_collection_type;
CURSOR emp_cur
IS SELECT *
FROM emp;
BEGIN
OPEN emp_cur;
LOOP
FETCH emp_cur
BULK COLLECT INTO l_emps
LIMIT 100;
EXIT WHEN l_emps.count = 0;
FOR i IN 1 .. l_emps.count
LOOP
<<do some complicated processing>>
END LOOP;
END LOOP;
END;
You can create a global temporary table (outside of the procedure) and use the global temporary table inside your procedure just as you would use any other table. So you can continue to use temporary tables if you so desire. But I can count on one hand the number of times I really needed a temporary table in Oracle.
You are right, temporary tables will work work you.
If you decide stick with regular tables you may want to use the advice #Johan gave, along with
ALTER TABLE <table name> NOLOGGING;
to make this perform a bit faster.
I see no problem in the scheme your are using.
Note that it doesn't have to be a temp-table, you can use a sort of kind of memory table as well.
Do this by creating a table as usual, then do
ALTER TABLE <table_name> CACHE;
This will prioritize the table for storage in memory.
As long as you fill and empty the table in short order you don't need to do step 1 & 2.
Remember the cache modifier is just a hint. The table still ages in the cache and will be pushed out of memory eventually.
Just do:
Populate cache-table with data through a stored procedure
Use the data in another stored procedure, but don't wait to long.
2a. Clear the data in the cache table.
In your MySQL version, I didn't see a step 5 to drop the table a. So, if you want or don't mind having the data in the table persist you could also use a materialized view and simply refresh on demand. With a materialized view you do not need to manage any INSERT statements, just include the SQL:
CREATE MATERIALIZED VIEW my_mv
NOCACHE -- NOCACHE/CACHE: Optional, cache places the table in the most recently used part of the LRU blocks
BUILD IMMEDIATE -- BUILD DEFERRED or BUILD IMMEDIATE
REFRESH ON DEMAND
WITH PRIMARY KEY -- Optional: creates PK column
AS
SELECT *
FROM ....;
Then in your other stored procedure, call:
BEGIN
dbms_mview.refresh ('my_mv', 'c'); -- 'c' = Complete
END;
That said, a global temporary table will work as well, but you manage the insert and exceptions.

Is MySQL Temporary table a shared resource?

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?
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.
Temporary table is visible only for current session.
So if you have several simultaneuous sessions - each one will have its own independent temporary table with the same name.
Documentation: http://dev.mysql.com/doc/refman/5.1/en/create-table.html, ctrl+f for "You can use the TEMPORARY"

temporary tables within stored procedures on slave servers with readonly set

We have set up a replication scheme master/slave and we've had problems lately because some users wrote directly on the slave instead of the master, making the whole setup inconsistent.
To prevent these problems from happening again, we've decided to remove the insert, delete, update, etc... rights from the users accessing the slave. Problems is that some stored procedure (for reading) require temporary tables.
I read that changing the global variable read_only to true would do what I want and allow the stored procedures to work correctly ( http://dev.mysql.com/doc/refman/5.0/en/server-system-variables.html#sysvar_read_only ) but I keep getting the error :
The MySQL server is running with the --read-only option so it cannot
execute this statement (1290)
The stored procedure that I used (for testing purpose) is this one :
DELIMITER $$
DROP PROCEDURE IF EXISTS `test_readonly` $$
CREATE DEFINER=`dbuser`#`%` PROCEDURE `test_readonly`()
BEGIN
CREATE TEMPORARY TABLE IF NOT EXISTS temp
(
`BT_INDEX` int(11),
`BT_DESC` VARCHAR(10)
);
INSERT INTO temp (BT_INDEX, BT_DESC) VALUES (222,'walou'), (111,'bidouille');
DROP TABLE temp;
END $$
DELIMITER ;
The create temporary table and the drop table work fine with the readonly flag - if I comment the INSERT line, it runs fine- but whenever I want to insert or delete from that temporary table, I get the error message.
I use Mysql 5.1.29-rc. My default storage engine is InnoDB.
Thanks in advance, this problem is really driving me crazy.
There seems to be a bug opened about this for the 6.0 beta:
http://bugs.mysql.com/bug.php?id=33669
[3 Jan 2008 19:26] Philip Stoev
Description: When the server is
started with --read-only, updates to
Falcon temporary tables are not
allowed.
You might want to add your findings there.