SQL: How to properly check if a record exists

前端 未结 9 1320
粉色の甜心
粉色の甜心 2020-11-28 01:23

While reading some SQL Tuning-related documentation, I found this:

SELECT COUNT(*) :

  • Counts the number of rows.
  • Often is improper
9条回答
  •  無奈伤痛
    2020-11-28 02:21

    You can use:

    SELECT 1 FROM MyTable WHERE... LIMIT 1
    

    Use select 1 to prevent the checking of unnecessary fields.

    Use LIMIT 1 to prevent the checking of unnecessary rows.

提交回复
热议问题