What is the complexity of this MySQL query
SELECT COUNT(*) FROM MyTable;
Is the count of the number of records in a table stored somewhere and updated every time a row is inserted or deleted? If so, then the complexity should be O (1).
It depends on the storage mechanism.
SELECT COUNT(*) FROM yourtable
From the manual:
InnoDB does not save the internal row count in the table. (In practice, this would be somewhat complicated due to multi-version programming.) To process the SELECT COUNT(*) FROM t , InnoDB must scan the table index, which takes some time if the index is not completely in the buffer pool. If your table does not change frequently, using the MySQL query cache is a good solution. To get a quick score, you need to use the counter table that you create, and let your application update it according to the inserts and delete it. SHOW TABLE STATUS can also be used if the approximate number of rows is sufficient. See Section 13.2.13.1, β InnoDB Performance Tuning Tips β.
InnoDB
SELECT COUNT(*) FROM t
SHOW TABLE STATUS
AFAIK in the array of strings MyISAM is cached, there isnβt any in InnoDB, and with every account he counts all the strings.
I am not sure if this value is saved or not, but it is not at all important for your request. Using MySQL with your query, it will read all returned rows at the time of its execution.