简体   繁体   English

检查记录是否存在的最干净方法

[英]Cleanest way to check if a record exists

I am using the following code to check if a row exists in my database: 我正在使用以下代码来检查数据库中是否存在行:

$sql = "SELECT COUNT(1) FROM myTable WHERE user_id = :id_var";
$stmt = $conn->prepare($sql);
$stmt->bindParam(':id_var', $id_var);
$stmt->execute();

if ($stmt->fetch()[0]>0)
{
    //... many lines of code
}

All of the code works and the doubts I have are concerning if the previous code is clean and efficient or if there is room for improvement. 所有代码都有效,并且我担心以前的代码是否干净高效,是否还有改进的余地。

Currently there are two questions bugging me with my previous code: 目前,有两个问题困扰着我之前的代码:

  1. Should I have a LIMIT 1 at the end of my SQL statement? 我的SQL语句结尾应该有LIMIT 1吗? Does COUNT(1) already limit the amount of rows found by 1 or does the server keep searching for more records even after finding the first one? COUNT(1)是否COUNT(1)找到的行数限制为1,或者服务器在找到第一个记录后仍继续搜索更多记录?
  2. The if ($stmt->fetch()[0]>0) . if ($stmt->fetch()[0]>0) Would this be the cleanest way to fetch the information from the SQL Query and execute the "if conditional"? 这是从SQL查询中获取信息并执行“如果有条件的话”的最干净的方法吗?

Of course if anyone spots anything else that can improve my code, I would love your feedback. 当然,如果有人发现任何可以改善我的代码的东西,我将很高兴收到您的反馈。

Q: Should I have a LIMIT 1 at the end of my SQL statement? 问:我的SQL语句结尾是否应该有LIMIT 1? Does COUNT(1) already limit the amount of rows found by 1 or does the server keep searching for more records even after finding the first one? COUNT(1)是否已将找到的行数限制为1,或者服务器在找到第一个记录后仍继续搜索更多记录?

Your SELECT COUNT() FROM query will return one row, if the execution is successful, because there is no GROUP BY clause. 如果执行成功,因为没有GROUP BY子句,则SELECT COUNT() FROM查询将返回一行。 There's no need to add a LIMIT 1 clause, it wouldn't have any affect. 无需添加LIMIT 1子句,不会有任何影响。

The database will search for all rows that satisfy the conditions in the WHERE clause. 数据库将搜索满足WHERE子句中条件的所有行。 If the user_id column is UNIQUE, and there is an index with that as the leading column, or, if that column is the PRIMARY KEY of the table... then the search for all matching rows will be efficient, using the index. 如果user_id列是UNIQUE,并且有一个索引作为前导列,或者,如果该列是表的PRIMARY KEY ...,那么使用索引来搜索所有匹配的行将非常有效。 If there isn't an index, then MySQL will need to search all the rows in the table. 如果没有索引,则MySQL将需要搜索表中的所有行。

It's the index that buys you good performance. 可以为您带来良好业绩的指标 You could write the query differently, to get a usable result. 您可以不同地编写查询,以获得可用的结果。 But what you have is fine. 但是你所拥有的很好。

Q: Is this the cleanest... 问:这是最干净的吗?

  if ($stmt->fetch()[0]>0)

My personal preference would be to avoid that construct, and break that up into two or more statements. 我个人的喜好是避免这种构造,并将其分解为两个或多个陈述。 The normal pattern...separate statement to fetch the row, and then do a test. 正常模式...单独的语句来获取行,然后进行测试。

Personally, I would tend to avoid the COUNT() and just get a row, and test whether there was a row to fetch... 就个人而言,我倾向于避免使用COUNT()而只获取一行,并测试是否有要获取的行...

  $sql = "SELECT 1 AS `row_exists` FROM myTable WHERE user_id = :id_var";
  $stmt = $conn->prepare($sql);
  $stmt->bindParam(':id_var', $id_var);
  $stmt->execute();
  if($stmt->fetch())  {
      // row found
  } else {
      // row not found 
  }
  $stmt->closeCursor(); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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