Question: I have table **Eli** with 1 million records. When I query the following:

```
Select count(*) from Eli where userId ='my_user'

```

It is taking more than 10 mins to give results. I searched web and found better way to optimize the query from `http://dbatipster.blogspot.com/2009/08/get-row-counts-fast.html`.

How do I utilize the the following query into my above query-

```
SELECT OBJECT_NAME(i.id) [Table_Name], i.rowcnt [Row_Count] 
FROM sys.sysindexes i WITH (NOLOCK) 
WHERE i.indid in (0,1) 
ORDER BY i.rowcnt desc

```

Answer: 
```
CREATE VIEW dbo.vwEli
WITH SCHEMABINDING
AS
    SELECT userId, cnt = COUNT(*)
    FROM Eli
GO

CREATE CLUSTERED INDEX ix ON dbo.vwEli (userId)
GO

SELECT cnt
FROM dbo.vwEli --WITH(NOEXPAND)
WHERE userId = 'my_user'

```
> 
> It is taking more than 10 mins to give results
> 
> 
> 

A cound over 1 million rows does not take that long. You bark the wrong tree. Totally. On a comical level - like complaining about the fuel consumption of a car with a broken tank leaking fuel. Seriously.

The only reason you would ever take that long is excessively bad locking - which is what you should fix. 1 million rows is nothing. Even on a 10 year old computer on a slow hard disc from that era it would not take even a minute to do your query. Something is blocking here. Likely other code written by a not competent programmer that is keeping around locks for way longer than it needs, possibly even excessive locks.