sql - Check if MySQL Table is empty: COUNT(*) is zero vs. LIMIT(0,1) has a result? -


this simple question efficiency related mysql implementation. want check if table empty (and if empty, populate default data) . best use statement select count(*) 'table' , compare 0, or better statement select 'id' 'table' limit 0,1 check if results returned (the result set has next)?

although need project working on, interested in how mysql works 2 statements , whether reason people seem suggest using count(*) because result cached or whether goes through every row , adds count intuitively seem me.

it better second method or exists. specifically, like:

if exists (select id table) 

should fastest way want. don't need limit; sql engine takes care of you.

by way, never put identifiers (table , column names) in single quotes.


Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -