我有多個進程運行完全相同的腳本,問題是他們正在選擇相同的數據。我有一個標誌「in_use」,它被選中後被設置爲1,但其他進程太快並且在腳本本身已經能夠將所有數據更新爲in_use = 1之前選擇相同的數據。我怎麼能,也許在查詢中,確保從未選擇相同的數據?以某種方式鎖定它?MySQL多個進程選擇相同的數據
現在我爲每個進程更新in_use,但它不夠快或不夠安全。
DELIMITER //
DROP PROCEDURE IF EXISTS `getDomainsForWhois`;
CREATE PROCEDURE `getDomainsForWhois`(
IN in_tld VARCHAR(10),
IN in_max_limit INT
)
BEGIN
SELECT
d.domainID,
CONCAT(SUBSTRING_INDEX(REPLACE(d.domainName, LOWER(d.tld), ''), '.' , -1), LOWER(d.tld)) as domainName,
d.tld
FROM
Domains d
WHERE (
d.parentDomainID IS NULL
AND
d.tld = in_tld
AND
d.dateFetched <= DATE_SUB(CURRENT_TIMESTAMP(),INTERVAL 1 DAY)
AND
d.in_use = 0
)
GROUP BY
domainName
ORDER BY
d.dateFetched ASC
LIMIT
0,in_max_limit
FOR
UPDATE;
END//
這基本上是我如何運行腳本。該腳本從多個進程(的cron)simaltaneously運行:
domains=`mysql -u USER -pPASSWORD DBNAME --skip-column-names -e "LOCK TABLE Domains WRITE; CALL getDomainsForWhois('.$1', $2)"`;
while read domainID domainName domainTld
do
mysql -u USER -pPASSWORD DBNAME -e "UPDATE Domains SET in_use = 1 WHERE domainID = $domainID";
done << EOF
$domains
EOF
mysql -u USER -pPASSWORD DBNAME -e "UNLOCK TABLES;";
你可以只使用select查詢嗎? '更新tbl設置MyLock = UniqueIDForPRocess然後從tbl中選擇*其中MyLock = UniqueIDForProcess – 2013-03-27 14:08:00