简体   繁体   中英

Tables getting locked in MySQL database

I am querying a MySQl db using mysql.connector in Python. the query is as follows:-

SELECT item.*
FROM d_table INNER JOIN
     i_table AS i
     ON (d_table.item_tag = i.item_tag) 
WHERE id = 20 AND
      timestamp >= '2000-06-30 00:00:00' AND
      timestamp <= '2001-07-30 00:00:00'  
GROUP BY i.id 

Here, the main problem is that the table is getting locked and since the table is getting queried every 0.2 seconds, is there a way to unlock the table or create a temporary table and return the results of the above query

I think the easiest way would be to make a view of the table. So you can select just the view.

CREATE VIEW test_view AS
SELECT item.*
FROM d_table INNER JOIN
     i_table AS i
     ON (d_table.item_tag = i.item_tag) 
WHERE id = 20 AND
      timestamp >= '2000-06-30 00:00:00' AND
      timestamp <= '2001-07-30 00:00:00'  
GROUP BY i.id;  

Then just:

select * from test_view;

This is a comment - but space limited in the coments section.

Here, the main problem is that the table is getting locked and since the table is getting queried every 0.2 seconds

Your asking the wrong questions in the wrong way.

Is there a performance impact arising from serialization of queries? If so, then table locking is just one part of the issue, the other is the time taken to run a query. You've provided no information to support any analysis of the perfomance question. While there are methods of avoiding locking, these can can translate into other performance issues.

You've provided no details of the table structures nor the distribution of the data nor the explain plan for the query.

With specific regard to table locking, its not possible to make any informaed suggestion without knowing what kind of locking is happenning. What is the database engine for these tables? What is the transaction isolation model? What is the workload?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM