How often should the indexes be rebuilt in our SQL Server database?

There’s a general consensus that you should reorganize (“defragment”) your indices as soon as index fragmentation reaches more than 5 (sometimes 10%), and you should rebuild them completely when it goes beyond 30% (at least that’s the numbers I’ve heard advocated in a lot of places). Michelle Ufford (a.k.a. “SQL Fool”) has an automated index … Read more

How can I make a SQL temp table with primary key and auto-incrementing field?

You are just missing the words “primary key” as far as I can see to meet your specified objective. For your other columns it’s best to explicitly define whether they should be NULL or NOT NULL though so you are not relying on the ANSI_NULL_DFLT_ON setting. CREATE TABLE #tmp ( ID INT IDENTITY(1, 1) primary … Read more

SQL Server : Transpose rows to columns

One way to do it if tagID values are known upfront is to use conditional aggregation SELECT TimeSeconds, COALESCE(MAX(CASE WHEN TagID = ‘A1’ THEN Value END), ‘n/a’) A1, COALESCE(MAX(CASE WHEN TagID = ‘A2’ THEN Value END), ‘n/a’) A2, COALESCE(MAX(CASE WHEN TagID = ‘A3’ THEN Value END), ‘n/a’) A3, COALESCE(MAX(CASE WHEN TagID = ‘A4’ THEN Value … Read more

SP taking 15 minutes, but the same query when executed returns results in 1-2 minutes

This is the footprint of parameter-sniffing. See here for another discussion about it; SQL poor stored procedure execution plan performance – parameter sniffing There are several possible fixes, including adding WITH RECOMPILE to your stored procedure which works about half the time. The recommended fix for most situations (though it depends on the structure of … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)