Nest Software

Nest Software Taking You Forward

24/05/2021

Find Slow Queries With SQL DMVs

There are several DMVs that provide data about query stats, ex*****on plans, recent queries and much more. These can be used together to provide some amazing insights.

For example, the query below can be used to find the queries that use the most reads, writes, worker time (CPU), etc.

SELECT TOP 10 SUBSTRING(qt.TEXT, (qs.statement_start_offset/2)+1,
((CASE qs.statement_end_offset
WHEN -1 THEN DATALENGTH(qt.TEXT)
ELSE qs.statement_end_offset
END - qs.statement_start_offset)/2)+1) query,
qs.ex*****on_count,
qs.total_logical_reads, qs.last_logical_reads,
qs.total_logical_writes, qs.last_logical_writes,
qs.total_worker_time,
qs.last_worker_time,
qs.total_elapsed_time/1000000 total_elapsed_time_in_S,
qs.last_elapsed_time/1000000 last_elapsed_time_in_S,
qs.last_ex*****on_time,
qp.query_plan
FROM sys.dm_exec_query_stats qs
CROSS APPLY sys.dm_exec_sql_text(qs.sql_handle) qt
CROSS APPLY sys.dm_exec_query_plan(qs.plan_handle) qp
ORDER BY qs.total_logical_reads DESC -- logical reads
-- ORDER BY qs.total_logical_writes DESC -- logical writes
-- ORDER BY qs.total_worker_time DESC -- CPU time

Nest Software

22/05/2021

★Microsoft SQL server suites★

Microsoft SQL Server is a relational database engine from Microsoft that has been supporting business applications for multiple decades.

SQL Server Management Studio (SSMS) - Data storage, management and real-time query processing.

SQL Server Integration Services (SSIS) - Perform export, import, transformation and loading of data.

SQL Server Reporting Services (SSRS) - Report authoring, management and delivery toolset.

SQL Server Analysis Services (SSAS) - Build, manage, analyze, aggregate and roll-up data for Business Intelligence.

SQL Server Agent - Scheduling and notification engine that ships with the relational engine.

Power BI data visualization - Reporting tool with both desktop and cloud based options.

SQL Server Business Intelligence(SSBI): Learn how to take complex data and turn it into business insights using a comprehensive, enterprise-ready business intelligence (BI) platform.

Nest Software

21/05/2021

Why Model DB:

The Model database is the template database that SQL Server uses to create new databases. Each time you create a new database in SQL Server, the contents of the Model database are copied to the new database to establish its default objects, including tables, stored procedures, and other database objects. The Model database is required even if you do not intend to create any new user databases. Each time SQL Server starts, the TempDB is recreated using the Model database as its template. By default, the Model database is empty when it is created.
Nest Software

21/05/2021

Why msdb:

The msdb database is a system database that is used by several SQL Server components such as the SQL Server Agent service. In addition to SQL Server Agent configuration and task information, replication, log shipping, and maintenance plan data are stored in the msdb database. The following list outlines the information contained in the msdb database:

■ SQL Server Agent information such as jobs, job history, job schedules, operators, alerts, and notifications

■ Database maintenance plan information such as maintenance plan jobs and history

■ Replication publishers and distributor information

■ Log shipping configuration and monitoring information
Nest Software

21/05/2021

Why Master DB:

The Master database is the primary configuration database in SQL Server. It contains information on all the databases that exist on the server, including the physical database files and their locations. The Master database also contains SQL Server’s configuration settings and login account information. The following list outlines the information contained in the master database:

■ Server Registrations and Remote Logins
■ Local Databases and Database Files
■ Login Accounts
■ Processes and Locks
■ Server Configuration Settings

Given its importance to SQL Server, a current backup of the Master database is critical to any server recovery.

21/05/2021

Why TempDB?

The TempDB system database plays an important role in SQL Server performance tuning process. Because it is used as caching storage to store different types of user database objects and to store the system internal objects in order to speed up the SQL Server Database Engine related processes.

Many times the people focus on the user databases to improve the performance, but sometimes the problem is not the user database itself. Sometimes the problem is the tempdb.

I want to try give an introduction about the tempdb database, show how to create some temporary objects there and show how to improve and monitor it.

The temdb is a special system database used to store temporary objects and data like tables, views table variables, tables returned in functions, temporary objects and indexes. The temdb can also be used for internal operations like rebuilding indexes (when the SORT_IN_TEMPDB is ON), queries using UNION, DBCC checks, GROUP BY, ORDER BY. Hash join and Hash aggregate operations.
But you must know,”The tempdb is in simple recovery model because the information stored is temporary.”
Creating temporary objects:

There are several temporary objects in SQL Server. Let’s start with the local temporary table.

Local temporary tables:
The following example creates a temporary local table in the tempdb database with the information of the Person.Address table.
SELECT [AddressID]
,[AddressLine1]
,[AddressLine2]
,[City]
,[StateProvinceID]
,[PostalCode]
,[SpatialLocation]
,[rowguid]
,[ModifiedDate]
into
FROM [Person].[Address]
If you check the tempdb, you will find the table in the temporary table folder.
The prefix # is used to indicate that it is a local temporary table. You can access to the temporary tables from any database. These tables or objects created are visible only in the session where they were created.

Global temporary tables:
These tables are global and can be accessed from other sessions which was not in possible in local temporary tables.
The syntax is similar than the local temporary tables, but with double # #:
SELECT [AddressID]
,[AddressLine1]
,[AddressLine2]
,[City]
,[StateProvinceID]
,[PostalCode]
,[SpatialLocation]
,[rowguid]
,[ModifiedDate]
into #
FROM [Person].[Address]

Now Have a look on table type variables:
The table variables can be used instead of the global and local temporary tables. They are easier to handle and to dispose the resources, which makes the use more efficient. The following example will show how to create the table variable, how to insert data on it and how to show the variable values. The use is similar to a simple table. The following example shows how to declare a table variable, how to insert data and how to do a select:

DECLARE TABLE (id INT, col1 varchar(20))
insert into values(1,'First value'),(2,'Second value')
select * from

You can also create it for globally DECLARE @ TABLE (id INT, col1 varchar(20))
As you can see, it is very easy to use them like any other variable. In general, the local temporary tables are more efficient. The table variables are also stored in the tempdb. However, depending on the scenarios you can only use table variables or local temporary tables. For example, if you need indexes, you can only do it with a local temporary table. By the other hand, in a function, you can only use a table variable. Note that the statistics are not maintained in table variables. In routines, table variables require fewer compiles.

A special type of table variable is the Table-value parameter. This is very useful for client applications.
The following example shows how to create and use Table-value parameters:
CREATE TYPE Table_value AS TABLE
(id int
, name varchar(30) );
GO

We will also create a simple table to fill with the Table-value parameter in a stored procedure:
CREATE table product
(id int
, productname varchar(30))

The stored procedure will load the data from the Table-value parameter into the product table created.
CREATE PROCEDURE insertdata
table_value READONLY
AS
SET NOCOUNT ON
INSERT INTO product
([id]
,productname)
SELECT id,name
FROM ;
GO

Now, we are going to declare the Table-value parameter insert data there and call the stored procedure and do a select in the table to make sure that the data was inserted:
DECLARE
AS Table_value;

INSERT INTO (id, name) values
(1,'Camera'),
(2,'iPad');

EXEC insertdata ;
GO

select * from product
If everything is OK, you should be able to see the new data inserted in the products table using the stored procedure.
Nest Software

20/05/2021

On every SQL Server instance there is a number of default system databases. Those are

master - keeps the information for an instance of SQL Server.
msdb - used by SQL Server Agent.
model - template database copied for each new database.
tempdb - keeps temporary objects for SQL queries.

Details are coming for every default system databases. stay with us. Nest Software

18/05/2021

MS Sql Server Read Log file

First of all, in order to be able to read any meaningful data your database needs to be in full recovery or bulk-logged mode. Otherwise you probably won't find much there.

Select * from fn_dblog(null,null)

RowLog Contents 0 to RowLog Contents 5 store all data

Ex: The data in RowLog Contents 0 is:

0x300008000100000002000001001200526564

Which can be broken down like this:

30 Status bit A
00 Status bit B
0800 Offset to find the number of columns in the row.
01000000 Data of fixed length col = 1
0200 Number of columns
00 Null bitmap
0100 Number of variable length columns
1200 Position where first variable length column ends, this is byte swapped which is 0x0012 which translates to 18.
526564 Data in variable length column like 'Dipankar Banik'

Run DML commands to see what is captured in SQL Server transaction log:
SELECT
[Current LSN],
[Transaction ID],
[Operation],
[Transaction Name],
[CONTEXT],
[AllocUnitName],
[Page ID],
[Slot ID],
[Begin Time],
[End Time],
[Number of Locks],
[Lock Information]
FROM sys.fn_dblog(NULL,NULL)
WHERE Operation IN
('LOP_INSERT_ROWS','LOP_MODIFY_ROW',
'LOP_DELETE_ROWS','LOP_BEGIN_XACT','LOP_COMMIT_XACT')

Finding internal SQL Server operations in transaction log:

--Get how many times page split occurs.
SELECT
[Current LSN],
[Transaction ID],
[Operation],
[Transaction Name],
[CONTEXT],
[AllocUnitName],
[Page ID],
[Slot ID],
[Begin Time],
[End Time],
[Number of Locks],
[Lock Information]
FROM sys.fn_dblog(NULL,NULL)
WHERE [Transaction Name]='SplitPage'
GO

--Get what all steps SQL Server performs during a single Page Split occurrence.
SELECT
[Current LSN],
[Transaction ID],
[Operation],
[Transaction Name],
[CONTEXT],
[AllocUnitName],
[Page ID],
[Slot ID],
[Begin Time],
[End Time],
[Number of Locks],
[Lock Information]
FROM sys.fn_dblog(NULL,NULL)
WHERE [Transaction ID]='any transaction id'

18/05/2021

View SQL server's last executed queries without SQL profiler

SELECT TOP 1000 db_name(qp.dbid) as databasename,sql_text.text as query,st.last_ex*****on_time
from sys.dm_exec_query_stats st
cross apply sys.dm_exec_sql_text(st.sql_handle) as sql_text
inner join sys.dm_exec_cached_plans cp
on cp.plan_handle=st.plan_handle
cross apply sys.dm_exec_query_plan(cp.plan_handle) as qp
order by st.last_ex*****on_time desc;

18/05/2021

MS SQL server Recovery Models

FULL recovery model
This recovery model logs every change to every row as well as a copy of each page added to indexes or table. As such the log contains enough information to be able to completely re- construct every action which occurred on the database, allowing you to restore your database back to any specific time, provided that you have a full log chain. All entries are kept in the online transaction log until the log is backed up, after which only active transactions will remain in the online log. This means that in order to get information about completed transactions from the log, the log backups will have to be taken into account.

BULK_LOGGED recovery model
When you are using the BULK_LOGGED recovery option, all minimally logged operations are not written to the Log. Minimally logged operations are operations such as SELECT INTO, BULK INSERT and Index operations. Essentially just enough information is logged to be able to undo the transaction, but not enough to redo it. The log is handled in much the same way as the FULL recovery model, and inactive transactions are moved to the log backup when a log backup is taken. Of course no information about bulk transactions are available.

SIMPLE recovery model
The SIMPLE recovery model only logs enough information to allow you to recover your database. All inactive log entries are automatically truncated when a checkpoint occurs. All operations are still being logged, but as soon as a checkpoint occurs the log is automatically truncated, which means that it becomes available for re-use and older log entries can now be over-written.

Configure Database Mail in SQL Server--SELECT DATABASE msdbsp_configure 'show advanced options', 1;GORECONFIGURE WITH OV...
16/05/2021

Configure Database Mail in SQL Server

--SELECT DATABASE msdb
sp_configure 'show advanced options', 1;
GO
RECONFIGURE WITH OVERRIDE;
GO
sp_configure 'Database Mail XPs', 1;
GO
RECONFIGURE WITH OVERRIDE;
GO
sp_configure 'show advanced options', 0;
GO
RECONFIGURE WITH OVERRIDE;
GO

-- Create a Database Mail profile
EXECUTE msdb.dbo.sysmail_add_profile_sp
= 'Notifications',
= 'Profile used for sending outgoing notifications using Gmail.' ;
GO

-- Grant access to the profile to the DBMailUsers role
EXECUTE msdb.dbo.sysmail_add_principalprofile_sp
= 'Notifications',
= 'public',
= 1 ;
GO

-- Create a Database Mail account as your mail server
EXECUTE msdb.dbo.sysmail_add_account_sp
= 'Dipankar',
= 'Mail account for sending outgoing notifications.',
= '[email protected]',
= 'Automated Mailer',
= 'smtp.xyz.com',
= 25,
-- = 0,
= '[email protected]',
= '123456' ;
GO

-- Add the account to the profile
EXECUTE msdb.dbo.sysmail_add_profileaccount_sp
= 'Notifications',
= 'Dipankar',
=1 ;
GO

--If for some reason, ex*****on of the code above returns an error, use the following code to roll back the changes:

EXECUTE msdb.dbo.sysmail_delete_profileaccount_sp = 'Notifications'
EXECUTE msdb.dbo.sysmail_delete_principalprofile_sp = 'Notifications'
EXECUTE msdb.dbo.sysmail_delete_account_sp = 'Dipankar'
EXECUTE msdb.dbo.sysmail_delete_profile_sp = 'Notifications'

--Test Database Mail configuration
EXEC msdb.dbo.sp_send_dbmail
= 'Notifications',
= '[email protected]',
= 'The database mail configuration was completed successfully.',
= 'Automated Success Message';
GO

--Troubleshooting Database Mail
sp_configure 'show advanced', 1;
GO
RECONFIGURE;
GO
sp_configure;
GO

To send e-mails, the user must also be a member of the DatabaseMailUserRole server role. Members of the sysadmin fixed server role and msdb db_owner role are members automatically. This can be easily checked by going to Security > Logins, right-click the current user and select Properties. In the Login Properties dialog click the ‘Server Roles’ page and make sure that the ‘sysadmin’ server role is checked

The Database Mail system logs e-mail activity in the ‘msdb’ database. To view the error messages returned by Database Mail, execute the following code:

SELECT * FROM msdb.dbo.sysmail_event_log;

Send e-mail from a trigger

USE AdventureWorks2014
GO

IF OBJECT_ID ('Production.iProductNotification', 'TR') IS NOT NULL
DROP TRIGGER Purchasing.iProductNotification
GO

CREATE TRIGGER iProductNotification ON Production.Product
FOR INSERT
AS
DECLARE nvarchar(255);
SELECT
= 'A new product, ' + Name + ', is now available for $' + CAST(StandardCost AS nvarchar(20)) + '!'
FROM
INSERTED i;
EXEC msdb.dbo.sp_send_dbmail
= 'Notifications',
= 'Use a valid e-mail address',
= ,
= 'New product notification'
GO

Some suggested troubleshooting steps:

Try to send a message through the same SMTP server, using the same machine as SQL Server, using a client mail program like Outlook. You may get a more meaningful error message.

The port may be blocked. Try sending mail to any other SMTP server using port 587.

Try changing to port 465.

Try unchecking the "requires a secure connection" bit in your profile.

Try using an SMTP server inside your network to relay to GMail. This might be easier to set up and get running. You'll need to install IIS and the SMTP components if you don't have a local SMTP server available to handle the relay.

You can open your messages from Gmail in other mail clients that support POP, like Microsoft Outlook. Step 1: Make sure POP is the best way to read your emails IMAP and POP are both ways to rea

12/05/2021

Importance of ACID Properties in DBMS

A transaction is a single logical unit of work which accesses and possibly modifies the contents of a database. Transactions access data using read and write operations.
In order to maintain consistency in a database, before and after the transaction, certain properties are followed. These are called ACID properties.

Atomicity :
By this, we mean that either the entire transaction takes place at once or doesn’t happen at all. There is no midway i.e. transactions do not occur partially. Each transaction is considered as one unit and either runs to completion or is not executed at all. It involves the following two operations.
—Abort: If a transaction aborts, changes made to database are not visible.
—Commit: If a transaction commits, changes made are visible.
Atomicity is also known as the ‘All or nothing rule’.

Consider the following transaction T consisting of T1 and T2: Transfer of 100 from account X to account Y.

BEFORE X: 500 AND Y: 200
Transection T
T1
Read(X)
X:=X-100
Write(X)
After: X:400

T2
Read(Y)
X:=Y+100
Write(Y)
After: X:300

If the transaction fails after completion of T1 but before completion of T2.( say, after write(X) but before write(Y)), then amount has been deducted from X but not added to Y. This results in an inconsistent database state. Therefore, the transaction must be executed in entirety in order to ensure correctness of database state.

Consistency :
This means that integrity constraints must be maintained so that the database is consistent before and after the transaction. It refers to the correctness of a database. Referring to the example above,
The total amount before and after the transaction must be maintained.
Total before T occurs = 500 + 200 = 700.
Total after T occurs = 400 + 300 = 700.
Therefore, database is consistent. Inconsistency occurs in case T1 completes but T2 fails. As a result T is incomplete.

Isolation
This property ensures that multiple transactions can occur concurrently without leading to the inconsistency of database state. Transactions occur independently without interference. Changes occurring in a particular transaction will not be visible to any other transaction until that particular change in that transaction is written to memory or has been committed. This property ensures that the ex*****on of transactions concurrently will result in a state that is equivalent to a state achieved these were executed serially in some order.
Let X= 500, Y = 500.
Consider two transactions T and T”.

T
Read(X)
X:=X*100
Write(X)
Read(Y)
Y:=Y-50
Write(Y)

T”
Read(X)
Read(Y)
Z:X+Y
Write(Z)

Suppose T has been executed till Read ( Y ) and then T’’ starts. As a result , interleaving of operations takes place due to which T’’ reads correct value of X but incorrect value of Y and sum computed by
T’’: (X+Y = 50, 000+500=50, 500)
is thus not consistent with the sum at end of transaction:
T: (X+Y = 50, 000 + 450 = 50, 450).
This results in database inconsistency, due to a loss of 50 units. Hence, transactions must take place in isolation and changes should be visible only after they have been made to the main memory.

Durability:
This property ensures that once the transaction has completed ex*****on, the updates and modifications to the database are stored in and written to disk and they persist even if a system failure occurs. These updates now become permanent and are stored in non-volatile memory. The effects of the transaction, thus, are never lost.

Address

1880, Muktadhara R/A, South Dania, Kadamtali
Dhaka
1236

Alerts

Be the first to know and let us send you an email when Nest Software posts news and promotions. Your email address will not be used for any other purpose, and you can unsubscribe at any time.

Contact The Business

Send a message to Nest Software:

Share