Hello,

I heard SELECT COUNT(*) can take a lot of resources if your counting a table with lots and lots of rows (hundred thousands, millions).

What if you add A WHERE clause to it? So something like:

SELECT COUNT(*) FROM table WHERE pid = ?
(pid is a index too btw)

IF adding a where clause, does it still scan the entire million plus rows, or only scans what is returned from the WHERE clause?

Any take on this is appreciated.

Thanks,
Bobby

Recommended Answers

All 2 Replies

if the column is indexed, it should only scan the rows included in the where clause

If you want to speed things up just use

SELECT COUNT(index_column) FROM table

This way you'll take advantage of indexes and their quickness.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.