Merge pull request #1468 from Neozonz/issue-1467

MySQL Optimization: always use order by when using limits
This commit is contained in:
Sebastian Grewe 2014-01-19 06:39:13 -08:00
commit 10e3fcab7e
5 changed files with 7 additions and 7 deletions

View File

@ -145,7 +145,7 @@ class Block extends Base {
* @return bool * @return bool
**/ **/
public function setConfirmations($block_id, $confirmations) { public function setConfirmations($block_id, $confirmations) {
$stmt = $this->mysqli->prepare("UPDATE $this->table SET confirmations = ? WHERE id = ?"); $stmt = $this->mysqli->prepare("UPDATE $this->table SET confirmations = ? WHERE id = ? LIMIT 1");
if ($this->checkStmt($stmt) && $stmt->bind_param("ii", $confirmations, $block_id) && $stmt->execute()) if ($this->checkStmt($stmt) && $stmt->bind_param("ii", $confirmations, $block_id) && $stmt->execute())
return true; return true;
return $this->sqlError(); return $this->sqlError();

View File

@ -60,7 +60,7 @@ class Payout Extends Base {
* @return boolean bool True or False * @return boolean bool True or False
**/ **/
public function setProcessed($id) { public function setProcessed($id) {
$stmt = $this->mysqli->prepare("UPDATE $this->table SET completed = 1 WHERE id = ?"); $stmt = $this->mysqli->prepare("UPDATE $this->table SET completed = 1 WHERE id = ? LIMIT 1");
if ($stmt && $stmt->bind_param('i', $id) && $stmt->execute()) if ($stmt && $stmt->bind_param('i', $id) && $stmt->execute())
return true; return true;
return $this->sqlError('E0051'); return $this->sqlError('E0051');

View File

@ -244,7 +244,7 @@ class Share Extends Base {
while ($affected > 0) { while ($affected > 0) {
// Sleep first to allow any IO to cleanup // Sleep first to allow any IO to cleanup
sleep($this->config['purge']['sleep']); sleep($this->config['purge']['sleep']);
$stmt = $this->mysqli->prepare("DELETE FROM $this->table WHERE id > ? AND id <= ? LIMIT " . $this->config['purge']['shares']); $stmt = $this->mysqli->prepare("DELETE FROM $this->table WHERE id > ? AND id <= ? ORDER BY id LIMIT " . $this->config['purge']['shares']);
$start = microtime(true); $start = microtime(true);
if ($this->checkStmt($stmt) && $stmt->bind_param('ii', $previous_upstream, $current_upstream) && $stmt->execute()) { if ($this->checkStmt($stmt) && $stmt->bind_param('ii', $previous_upstream, $current_upstream) && $stmt->execute()) {
$affected = $stmt->affected_rows; $affected = $stmt->affected_rows;

View File

@ -567,7 +567,7 @@ class Statistics extends Base {
* @param worker_id int Worker ID to fetch hashrate for * @param worker_id int Worker ID to fetch hashrate for
* @return data int Current hashrate in khash/s * @return data int Current hashrate in khash/s
**/ **/
public function getWorkerHashrate($worker_id) { public function getWorkerHashrate($worker_id,$interval=600) {
$this->debug->append("STA " . __METHOD__, 4); $this->debug->append("STA " . __METHOD__, 4);
if ($data = $this->memcache->get(__FUNCTION__ . $worker_id)) return $data; if ($data = $this->memcache->get(__FUNCTION__ . $worker_id)) return $data;
$stmt = $this->mysqli->prepare(" $stmt = $this->mysqli->prepare("
@ -576,7 +576,7 @@ class Statistics extends Base {
" . $this->user->getTableName() . " AS u " . $this->user->getTableName() . " AS u
WHERE u.username = SUBSTRING_INDEX( s.username, '.', 1 ) WHERE u.username = SUBSTRING_INDEX( s.username, '.', 1 )
AND our_result = 'Y' AND our_result = 'Y'
AND s.time > DATE_SUB(now(), INTERVAL 600 SECOND) AND s.time > DATE_SUB(now(), INTERVAL ? SECOND)
AND u.id = ?"); AND u.id = ?");
if ($this->checkStmt($stmt) && $stmt->bind_param("i", $account_id) && $stmt->execute() && $result = $stmt->get_result() ) if ($this->checkStmt($stmt) && $stmt->bind_param("i", $account_id) && $stmt->execute() && $result = $stmt->get_result() )
return $this->memcache->setCache(__FUNCTION__ . $worker_id, $result->fetch_object()->hashrate); return $this->memcache->setCache(__FUNCTION__ . $worker_id, $result->fetch_object()->hashrate);

View File

@ -31,7 +31,7 @@ class Worker extends Base {
} }
// Prefix the WebUser to Worker name // Prefix the WebUser to Worker name
$value['username'] = "$username." . $value['username']; $value['username'] = "$username." . $value['username'];
$stmt = $this->mysqli->prepare("UPDATE $this->table SET password = ?, username = ?, monitor = ? WHERE account_id = ? AND id = ?"); $stmt = $this->mysqli->prepare("UPDATE $this->table SET password = ?, username = ?, monitor = ? WHERE account_id = ? AND id = ? LIMIT 1");
if ( ! ( $this->checkStmt($stmt) && $stmt->bind_param('ssiii', $value['password'], $value['username'], $value['monitor'], $account_id, $key) && $stmt->execute()) ) if ( ! ( $this->checkStmt($stmt) && $stmt->bind_param('ssiii', $value['password'], $value['username'], $value['monitor'], $account_id, $key) && $stmt->execute()) )
$iFailed++; $iFailed++;
} }
@ -259,7 +259,7 @@ class Worker extends Base {
**/ **/
public function deleteWorker($account_id, $id) { public function deleteWorker($account_id, $id) {
$this->debug->append("STA " . __METHOD__, 4); $this->debug->append("STA " . __METHOD__, 4);
$stmt = $this->mysqli->prepare("DELETE FROM $this->table WHERE account_id = ? AND id = ?"); $stmt = $this->mysqli->prepare("DELETE FROM $this->table WHERE account_id = ? AND id = ? LIMIT 1");
if ($this->checkStmt($stmt) && $stmt->bind_param('ii', $account_id, $id) && $stmt->execute() && $stmt->affected_rows == 1) if ($this->checkStmt($stmt) && $stmt->bind_param('ii', $account_id, $id) && $stmt->execute() && $stmt->affected_rows == 1)
return true; return true;
return $this->sqlError('E0061'); return $this->sqlError('E0061');