This document provides an overview of key topics for database training for developers, including database design, creating databases and database objects, writing efficient stored procedures, and optimizing queries. It discusses modeling data through entity relationship diagrams, creating tables, views, indexes and stored procedures. It also offers tips for writing well-structured stored procedures and optimizing query performance through execution plans and tuning techniques.
2. *Go through*
• Database Design.
• Create Database.
• Create User.
• Grant User Privileges.
• Create Database Objects.
• Apply Indexes on tables.
• Efficient way to write stored procedures.
• Execution Plan/Explain Plan for Query
Optimization.
3. Design of database.
• Understand the requirement of the system.
• Read document and imagine about growth of
database and relation with each entity
• Create E-R Diagrams
4. Create Database.
• Create Database based on understanding of
system.
• Use E-R diagram to create objects.
5. Create User.
• Every database connected /accessed by users
and logins.
• Create User with necessary privileges to
connect and operation on database.
6. Create Database Objects
• Create tables with necessary columns. Add at least one
Primary key and if system generated then put auto
incremented.
• Create Views based on Table. Put hint WITH (NOLOCK)
.Grant Select permission to View not Table for security
reason.
• Created extra non-clustered index on most accessed
columns.
• Use SSMS Performance monitor after running database.SQL
Server gather statistics and get the information of missing
indexes. Based on this statistics also can indexes creation
possible after release of system.
• Add description of tables and columns.
7. How to write stored procedures
• Minimum sized header of stored procedure. It should be limited
text.
• For e.g.
•
/*********************************************
• *Created By:Bhavesh Thakkar
• *Description:”Wow” It’s too short.
• *Modified Info:2013-01-29..Oh..! God Still small size require in it.
• *execution code.must return value.for only select statement.not for
DML
•
• */
• Continued..
8. How to write stored procedures
• 2. Deallocate/Close Cursor at the end of sps.
• 3. Try to avoid Cursors in stored procedure.
• 4. Use proper naming to declare.
• Select * into #AllJPG from tmf_meterfiles
where FileType=1
• Declare @SQLQuery
• Declare @SQLExtension
• 5. Do not use * instead of column name in
Select Query.
• 6. Drop # (hash) tables at the end of query.
9. How to write stored procedures
• 7. Use functions to avoid complexity.
• Suppose, Get Film Name producer name and billing amount
•
• --Without Function
• Select f.filmname,fw.WorkerName,b.totalammount
• From dbo.tBilling b inner join dbo.tfilm f
• on b.filmid=f.filmid
• inner join dbo.tFilmWorkers fw
• on f.ProducerId=fw.wid
•
• --With Function
•
• Select dbo.fnGetFilmName(filmId) FilmName,dbo.fnGetWorkerName(WID)
WorkerName,b.totalammount
• From dbo.tBilling b
10. Efficient way to optimize queries and
stored procedures
• This topic become much easier when we write
stored procedures and queries proper way
that discussed in topic 7.
• Still, Query consumes memory and execution
time more then we should go with Query
tuning.
• Follow these steps...
• Continued..
11. Efficient way to optimize queries and
stored procedures
• Check Estimated Execution/Explain Plan
12. Efficient way to optimize queries and
stored procedures
• 2. Check indexes on tables that used in
queries.
• 3. Check enough memory allocated to SQL
Server.
• 4. Create Indexed views if useful to your
query.
• 5. Partition Large sized tables.
13. Efficient way to optimize queries and
stored procedures
• --[usp_GetTvTrackDetails_Android] 'Mexico',10, 7462, '2012-OCT-01 6:00:00 AM'
• SET STATISTICS TIME ON
• Declare @CountryName as varchar(50),
• @LiveMinDiff as int,
• @BlankImgSize as int=7462,
• @StartDateTime as smalldatetime,
• @InActiveMeterFlagDays as int = -2
•
• Declare @EndDateTime as smalldatetime
• SET @LiveMinDiff=10
• SET @CountryName='Mexico'
• SET @StartDateTime='2012-MAY-01 6:00:00 AM'
•
• Declare @EndDateTime as smalldatetime
• Set @EndDateTime=dateadd(Day,1,@StartDateTime)
• --Get Footer Value
•
• -- RowNo,MeterNo,TheFileName,TheFilePath,FileDateTime,FileSize
• select * into #Temp_MeterFiles from tMF_MeterFiles WITH(NOLOCK) where MeterNo IN (SELECT MeterNo FROM TMETERS WHERE COUNTRY=@CountryName and IsConfirm=1)
• And FILEDATETIME between @StartDateTime and @EndDateTime and filedatetime<= getutcdate()
•
• Select * from
• (select Count(RowNo) as ImgCount from #Temp_MeterFiles where TheFileName LIKE '%.jpg')T1
• ,
• (select count(distinct MeterNo) as AvtiveHomes from #Temp_MeterFiles where TheFileName LIKE '%.jpg')T2
• ,
• (select count(distinct MeterNo) as ActiveMeters from #Temp_MeterFiles)T3
• ,
• (select count(distinct MeterNo) as LiveHomes from #Temp_MeterFiles where TheFileName LIKE '%.jpg' AND dateDiff(minute,FILEDATETIME,getutcdate())<=@LiveMinDiff)T4
• ,
• (select Count(*) as MeterCount from tmeters where country=@CountryName)T5
• ,
• (Select Count(MeterNo) as ConfirmMeterCount from tMeters where IsConfirm=1)T7
•
•
• Select top 1 TheFileName,TheFilePath from #Temp_MeterFiles where TheFileName LIKE '%.jpg' and FileSize>@BlankImgSize order by FileDateTime desc
•
•
• Select distinct MeterNo from #Temp_MeterFiles where TheFileName LIKE '%.jpg' AND dateDiff(minute,FILEDATETIME,getutcdate())<=@LiveMinDiff
•
• select meterNo as InActiveMeters into #Temp_InActiveMeters from tMeters where country=@countryName and MeterNo not in (Select MeterNo from #Temp_MeterFiles) and
IsConfirm=1
•