Showing posts with label Sql Server. Show all posts
Showing posts with label Sql Server. Show all posts

Tips to Increase performance of SQL Server

0 comments
Don’t use the * in your queries. A SELECT * creates a overload on table, Input/Output and network bandwidth.

All columns involved in indexes should appear on WHERE and JOIN clauses on the same sequence they appear on index.

Avoid VIEWs. Use them only when there are benefits of doing so.

Verify if a critical query gains performance by turning it in a stored procedure.

Database Sql server to check or monitor or trace the free space/used/ file size in MB

1 comments
Database to check or monitor free space available

select convert(decimal(12,2),round((a.size-fileproperty(a.name,'SpaceUsed'))/128.000,2)) as FreeSpaceMB from dbo.sysfiles a where fileid = 1;

Database to check or monitor File size MB

select convert(decimal(12,2),round(a.size/128.000,2)) as FileSizeMB from dbo.sysfiles a where fileid = 1;

Database to check or monitor Space Used size in MB

select convert(decimal(12,2),round(fileproperty(a.name,'SpaceUsed')/128.000,2)) as SpaceUsedMB from dbo.sysfiles a where fileid = 1;

Asp.net Membership tables auto growing when you use shopping cart sites. Use this query to delete records

0 comments
Asp.net Membership tables will grow automatically when you use shopping cart sites. After some period the database size will full and you will get the error as

"Could not allocate space for object 'dbo.aspnet_Profile'.'PK__aspnet_Profile__2057CCD0' in database '' because the 'PRIMARY' filegroup is full. Create disk space by deleting unneeded files, dropping objects in the filegroup, adding additional files to the filegroup, or setting autogrowth on for existing files in the filegroup."

So to delete use the below query in stored procedures or by using triggers to perform delete operation

DECLARE @Days int
SET @Days = 21
DECLARE @NoOfUsersToDelete int
SET @NoOfUsersToDelete = 1000


IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[PagesToDelete]') AND type in (N'U'))
DROP TABLE [dbo].[PagesToDelete]
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[aspnetUsersToDelete]') AND type in (N'U'))
DROP TABLE [dbo].[AspnetUsersToDelete]
create table PagesToDelete (PageID int NOT NULL PRIMARY KEY)
create table AspnetUsersToDelete (UserID uniqueidentifier NOT NULL PRIMARY KEY)

insert into AspnetUsersToDelete
select top(@NoOfUsersToDelete) UserID from aspnet_Users where
(isAnonymous = 1) and (LastActivityDate < (getDate()-@Days))
order by UserID
print 'Users to delete: ' + convert(varchar(255),@@ROWCOUNT)
GO
insert into PagesToDelete
select ID from Page where UserID in
(
select UserID from AspnetUsersToDelete
)
print 'Pages to delete: ' + convert(varchar(255),@@ROWCOUNT)
GO

delete from WidgetInstance where PageID IN
( SELECT PageID FROM PagesToDelete )

print 'Widget Instances deleted: ' + convert(varchar(255), @@ROWCOUNT)
GO

delete from Page where ID IN
( SELECT PageID FROM PagesToDelete )
GO
delete from UserSetting WHERE UserID IN
( SELECT UserID FROm AspnetUsersToDelete )
GO
delete from aspnet_Profile WHERE UserID IN
( SELECT UserID FROm AspnetUsersToDelete )
GO
delete from aspnet_UsersInRoles WHERE UserID IN
( SELECT UserID FROm AspnetUsersToDelete )
GO
delete from aspnet_PersonalizationPerUser WHERE UserID IN
( SELECT UserID FROm AspnetUsersToDelete )
GO
delete from aspnet_users where userID IN
( SELECT UserID FROm AspnetUsersToDelete )
PRINT 'Users deleted: ' + convert(varchar(255), @@ROWCOUNT)
GO
drop table PagesToDelete
drop table AspnetUsersToDelete

GO
=================================
Second method by using triggers to delete
=================================
CREATE TRIGGER DELETE_Aspprofileanduserstable
on aspnet_Profile
after update
as
begin
Delete from aspnet_profile
where userid in ( select userid from aspnet_users
where isanonymous = 1
and datediff(dd, lastactivitydate, getdate())> 14)


Delete from aspnet_users
where userid in ( select userid from aspnet_users
where isanonymous = 1
and datediff(dd, lastactivitydate, getdate())> 14)

end

go

Execute SqlServer script using command prompt

0 comments
Execute SQL Server script using command prompt.


sqlcmd -S myServer\instanceName -U sa -P sa1234 -d Northwind -i C:\myScript.sql


Sql server query to find all tables and space they used

0 comments

This query is used to find all tables and space they used


SELECT sum ( used_page_count ) * 8 as SizeKB,
  sum(row_count) as [RowCount], object_name ( object_id ) AS TableName
FROM sys.dm_db_partition_stats
WHERE index_id=0 or index_id=1
GROUP BY object_id
ORDER BY sum ( used_page_count ) DESC

Vikram Chandra Theme by BloggerThemes & NewWPThemes Sponsored by iBlogtoBlog