Skip to main content

How to lock tables with codeigniter?

I have to run this sql routine in a model:

$this->db->query('LOCK TABLE orders WRITE');
$this->db->query('TRUNCATE TABLE orders');
$this->db->query('INSERT INTO orders SELECT * FROM orders_tmp');
$this->db->query('UNLOCK TABLES');

but I get this error:

Error Number: 1192

Impossible to execute the requested command: tables under lock or transaction running

TRUNCATE TABLE orders

I use MyISAM as DB engine on this table.

Could you please help me?

Solved

To perform many INSERT and SELECT operations on a table real_table when concurrent inserts are not possible, you can insert rows into a temporary table temp_table and update the real table with the rows from the temporary table periodically. This can be done with the following code:

mysql> LOCK TABLES real_table WRITE, temp_table WRITE;

Kindly ask if it not worked for you.


try this

$this->db->query('TRUNCATE TABLE orders');
$this->db->query('LOCK TABLE orders WRITE');
$this->db->query('INSERT INTO orders SELECT * FROM orders_tmp');
$this->db->query('UNLOCK TABLES');

You're required to acquire a lock for all tables in your query, not just the table you're writing to. So in your case you also need a read lock on orders_tmp.

From the docs:

A session that requires locks must acquire all the locks that it needs in a single LOCK TABLES statement. While the locks thus obtained are held, the session can access only the locked tables.

Docs here: https://dev.mysql.com/doc/refman/5.5/en/lock-tables.html

Cheers


Comments

Popular posts from this blog

Mobile menu created with Bootstrap 3

// Change positioning of mobile menu icon based on screen size. $(document).ready(function(){ if($(window).width() Toggle navigation Home About Services Location Gallery Contact I have a mobile menu created with Bootstrap 3 that automatically aligns to the right side of screen. I wrote some JavaScript code to make mobile menu icon appear on the left side of screen because I have a Facebook button on the right side of screen. Everything works with my code; however, there is one area I'm trying to improve. On my gallery page I have 500+ images. The problem is my mobile menu stays on right side and pushes the Facebook button slightly over to the left until the page fully loads. After the page fully loads everything appears correctly. How ca...

Vacuum does not reclaim disk space

I have a fact table with 9.5M records. The table uses distyle=key, and is hosted on a RedShift cluster with 2 "small" nodes. I made many UPDATE and DELETE operations on the table, and as expected, I see that the "real" number of rows is much above 9.5M. Hence, I ran vacuum on the table, and to my surprise, after vacuum finished, I still see that the number of "rows" the table allocates did not come back to 9.5M records. Could you please advice what may be a reason for such a behavior? What would be the best way to solve it? A little bit of copy-pastes from my shell: The fact table I was talking about: select count(1) from tbl_facts; 9597184 The "real" number of records in the DB: select * from stv_tbl_perm where id= 332469; slice | id | name | rows | sorted_rows | temp | db_id | insert_pristine | delete_pristine -------+--------+--------------------------------------------------------------------------+----------+-------------...