table-locking

How SQL Server handles UPDATE transactions?

。_饼干妹妹 提交于 2019-12-24 03:14:13
问题 We have a table which is used to Generate Unique Numeric keys. These keys are then used as a PrimaryKey in other tables. Table structure is like this: TableName VARCHAR CurrentKey INT So we have data in this table like TableName Customers CurrentKey 400 So when we need next primary key for table Customers we get the CurrentKey from this table where TableName is Customers , it will give us 400 we increment (400+1) it and we update this key in the table also. So our CurrentKey is 401 now. The

SQL Server - Merging large tables without locking the data

岁酱吖の 提交于 2019-12-22 03:45:16
问题 I have a very large set of data (~3 million records) which needs to be merged with updates and new records on a daily schedule. I have a stored procedure that actually breaks up the record set into 1000 record chunks and uses the MERGE command with temp tables in an attempt to avoid locking the live table while the data is updating. The problem is that it doesn't exactly help. The table still "locks up" and our website that uses the data receives timeouts when attempting to access the data. I

How to explicitly lock a table in Microsoft SQL Server (looking for a hack - uncooperative client)

ぐ巨炮叔叔 提交于 2019-12-18 21:22:32
问题 This was my original question: I am trying to figure out how to enforce EXCLUSIVE table locks in SQL Server. I need to work around uncooperative readers (beyond my control, closed source stuff) which explicitly set their ISOLATION LEVEL to READ UNCOMMITTED. The effect is that no matter how many locks and what kind of isolation I specify while doing an insert/update, a client just needs to set the right isolation and is back to reading my garbage-in-progress. The answer turned out to be quite

Locking a table with a select in Entity Framework

时间秒杀一切 提交于 2019-12-18 07:45:08
问题 I need to do something like this select * from myTable with (xlock,holdlock) using Entity Framework. Is this possible? I've opened a TransactionScope with the Serializable isolation level but my selects are not locking the tables. I'd like them to lock until I complete the transaction scope. 回答1: It is possible but you have to issue the SQL you can't add the locking hint when using LINQ (as far as I know): ObjectContext.ExecuteStoreCommand( string.Format("select 1 from [{0}] with (tablockx,

InnoDB only insert record if referenced id exists (without FOREIGN KEYS)

百般思念 提交于 2019-12-11 03:41:26
问题 Foreign keys may be the best approach for this problem. However, I'm trying to learn about table locking/transactions, and so I'm hoping that we can ignore them for the moment. Let's pretend that I have two tables in an InnoDB database: categories and jokes ; and that I'm using PHP/MySQLi to do the work. The tables look like so: CATEGORIES id (int, primary, auto_inc) | category_name (varchar[64]) ============================================================ 1 knock, knock JOKES id (int,

mysql table locked after php crashes

ぐ巨炮叔叔 提交于 2019-12-10 23:13:40
问题 I have a MySQL DB and an innoDB table in it. I have a php page that connects, locks the table, does some updates, then unlocks the table. The PHP page is being served up with apache via wamp. The php page uploads a file to the database. I decided to simulate the system crashing by uploading a file that has a size larger than the memory that is allocated to PHP. This definitely caused this error: allowed memory size of 18874368 bytes exhausted (tried to allocate 6176754 bytes). After that, the

MySQL 5.6 - table locks even when ALGORITHM=inplace is used

主宰稳场 提交于 2019-12-08 16:14:30
问题 I'm running the following ALTER command on a MySQL 5.6 database on a large table with 60 million rows: ALTER TABLE `large_table` ADD COLUMN `note` longtext NULL, ALGORITHM=INPLACE, LOCK=NONE; Despite specifying both ALGORITHM=INPLACE and LOCK=NONE , the table gets locked and essentially takes down the app until the migration is complete. I verified that the table was indeed locked by checking the value of the In_use column on the output of the SHOW OPEN TABLES command. It was set to 1 . From

MySQL select with a count query on another table

ぃ、小莉子 提交于 2019-12-08 01:50:51
问题 I have simple article application with three tables: article id, title, body, user_id comment id, article_id, user_id, body user id, username On the landing page, I want to show the latest article titles with the author name and total numbers of comments of the article. The main problem is how to get total numbers of comments of the article,I did not get it right. I should get the following output: title username total_comments article 2 user2 0 article 1 user1 2 In my real application, I

MySQL select with a count query on another table

馋奶兔 提交于 2019-12-06 07:28:34
I have simple article application with three tables: article id, title, body, user_id comment id, article_id, user_id, body user id, username On the landing page, I want to show the latest article titles with the author name and total numbers of comments of the article. The main problem is how to get total numbers of comments of the article,I did not get it right. I should get the following output: title username total_comments article 2 user2 0 article 1 user1 2 In my real application, I added a column in article table for total number of comments to the article. this column is updated when a

How innodb tables are locked when ON INSERT trigger is processed?

浪子不回头ぞ 提交于 2019-12-03 13:46:35
问题 I have two innodb tables: articles id | title | sum_votes ------------------------------ 1 | art 1 | 5 2 | art 2 | 8 3 | art 3 | 35 votes id | article_id | vote ------------------------------ 1 | 1 | 1 2 | 1 | 2 3 | 1 | 2 4 | 2 | 10 5 | 2 | -2 6 | 3 | 10 7 | 3 | 15 8 | 3 | 12 9 | 3 | -2 When a new record is inserted into the votes table, I want to update the sum_votes field in articles table by calculating the sum of all votes. The question Which way is more efficient, if the SUM()