Translate

Showing posts with label Development. Show all posts
Showing posts with label Development. Show all posts

Monday, August 30, 2021

Physical Join Operators in SQL Server

Every developer knows the Logical Operators such as Inner Join, Left Outer, Right Outer Join etc in SQL Server. 


Do you how these operators work internally. This article discusses the Internals of Physical Join Operators (Nested Loops Join, Hash Match Join & Merge Join) in SQL Server.



Monday, January 11, 2021

Another Confrontation for Indexes

Source: https://medium.com/analytics-vidhya/anatomy-index-in-relational-db-a1425f2d8a02

Indexes are seemed to be a never-ending topic. It is very difficult to convince application developers regarding the indexes. This is another instance of such. 

A user complained saying that they are getting continuous timeouts from their application during some processing. They insisted that it is a problem with the server resources. Since the server in question is housing more than 30 databases, that cannot be the reason. Further, even this application is working nicely with other queries but not with one particular query. When the requested for the particular query, it was not provided as the basic conclusion is that this is due to the server resources. Then the development team insisted to have a look on the database server which we did. At that time, server memory was 97% with CPU is at 4%. So, naturally, all were pointing the guns at server memory. The server memory is something that I have been elaborating to the users over the years, but with little success. It was very difficult to make them believe that database is a different animal is altogether. 

Finally, I got the table name even though I could not get the exact query. Well, the table has more than 300,000 records with one clustered index on the identity column. I was told that there process this table for each user. They were having around 1,500 users. That means the query in question, is doing table scans of 300,000 * 1,500. This table had more than 100+ columns but still, I couldn't make them satisfy that this an index problem. Then I offered them to delete a few records and check. Well, with fewer data their query worked nicely. Finally, they accepted it was an index issue, but for that more than 4-5 hrs were spent like the previous incident on the index.

Learnings from the incident

#1 - Increasing the hardware resource will not the first solution, there can be more other options than that. 

#2 - Don't be panic seeing that the server memory of a database server is hitting 90+%. That is how it is. You should worry if it is not. 

#3 - Index can do wonders for you only if you know the index concepts. 

If you need more details on Index read this article.

Wednesday, August 28, 2019

SQL Server ALTER TABLE ADD Column overview


In this article, we will explore SQL Server ALTER TABLE ADD Column statements to add column(s) to an existing table. We will also understand the impact of adding a column with a default value and adding and updating the column with a value later on larger tables.

Saturday, November 17, 2018

SQL Server Temporal Tables Overview

For many applications, it's a common requirement to get details about data modifications that have occurred in a SQL Server table. Tables can have multiple updates and deletes for different reasons, and there may be a need to keep the different versions of records. Prior to SQL Server 2016 you would have to use triggers to keep track of data changes in tables, but now we can use the built-in temporal table feature that was first released in SQL Server 2016.

Read more at https://www.mssqltips.com/sqlservertip/5781/sql-server-temporal-tables-overview/

Saturday, November 11, 2017

New T-SQL Commands in SQL Server 2017

Despite the maturity of TSQL, SQL Server 2017 still introduces several new features which to the T-SQL family which can be very useful in database development.
http://www.sql-server-performance.com/2017/new-t-sql-commands-in-sql-server-2017/


Tuesday, May 30, 2017

CAST and TRY_CAST

We always face issues with casting. When we write casting we expect, it to be in required data type. However, often we see there error with CAST when the correct data is not provided. This might cause entire process to fail.


This issue can be avoided in SQL Server 2012 with TRY_CAST.





Friday, November 18, 2016

CREATE OR ALTER In One Statment

When ever we execute stored procedure script, we always drop the existing one and recreate it.

IF EXISTS (SELECT * FROM sys.procedures WHERE Name = 'Test')
BEGIN
DROP PROC Test
END
GO

CREATE PROCEDURE Test
AS
BEGIN
       SELECT 1
END

However, with SQL Server 2016 SP1, now there is easy way of doing with one statement.

CREATE OR ALTER PROCEDURE Test
AS
BEGIN
         SELECT 1
END


Obvious advantage is this is much cleaner now. This feature is available for Functions, Triggers and Views as well. 

There is an another important benefits out of this.

If you check the stored procedure stats for the drop and create method by running the following query, you will end up with below results. 

SELECT name,create_date,modify_date
 FROM sys.procedures WHERE Name = 'Test'


In this, both the create and the modify dates are same. However, for the new method, now it has a different modify date which means you can track your objects much better. 






Thursday, June 23, 2016

Disable Column Store Indexes for One Database

Disabling column store indexes needs to be done time to time. Following query will generate the script. 

SELECT 'ALTER INDEX [' + Name + +'] ON ' + OBJECT_NAME(object_id) + ' DISABLE'
FROM sys.indexes WHERE TYPE_DESC
LIKE '%COLUMNSTORE%'

Output of the query is shown below.

ALTER INDEX [IndFactResellerSalesXL_CCI] ON FactResellerSalesXL_CCI DISABLE
ALTER INDEX [NonClusteredColumnStoreIndex-20160622-204327] ON DimEmployee DISABLE
ALTER INDEX [NonClusteredColumnStoreIndex-20160622-210321] ON FactInternetSales DISABLE

Saturday, September 5, 2015

Thursday, June 18, 2015

SQL Server 2016: Dynamic Data Masking

Row-level security is great for all or nothing decisions, but there are times when users need access to a subset of the data. For example, the last four digits of a credit card or social security number. This can be done at the application level, but that leaves room for error. You only need to forget the mask one time to leak sensitive data.

SQL Server 2016 attempts to address this with a feature called Dynamic Data Masking. When a column is created with a mask, it defaults to returning only the data exposed through the mask. There are three types of masks currently available:

Read more at http://www.infoq.com/news/2015/06/SQL-Server-Masking

Tuesday, June 16, 2015

How to Load Data From PDF file in SSIS

This is the question some one asked today. I haven’t done this but I can remember I read this some where. So thought of digging this today.

Some forums are suggesting a product.

http://www.datawatch.com/monarch-is-back/

Out of the box, Datawatch Monarch can work with a wide range of report formats including PDF, XML, HTML, text, spool and ASCII files. Access data from invoices, sales reports, balance sheets, customer lists, inventory, logs and more. The system is easy to use, allowing you to quickly select a file and automatically convert it into structured data for analysis.

Following blog includes how to import several types to SQL Server.

http://sqlage.blogspot.com/2013/12/ssis-how-to-import-files-text-pdf-excel.html

Following blogspot also have lot of links

http://www.forumarray.com/ssis-data-flow-source-component-to-read-a-pdf-file-226359

Following blog is the most relevant one for me. http://sql31.blogspot.com/2013/03/how-to-load-data-from-pdf-file-in-ssis.html

This users script task and you have the option of customizing it.

Thursday, June 4, 2015

New Couchbase Release Bridges SQL-NoSQL Database Worlds

NoSQL database vendor Couchbase has developed a SQL query language that will help developers with SQL expertise -- and there are millions of them -- build enterprise web, mobile and Internet-of-Things applications that run on the Couchbase Server. Couchbase is previewing the new N1QL (pronounced "nickel") language at its Couchbase Connect user conference in Santa Clara, Calif., this week. The company also is previewing a beta of the 4.0 release of Couchbase Server at the event.

Read more at http://www.crn.com/news/applications-os/300077012/new-couchbase-release-bridges-sql-nosql-database-worlds.htm

Saturday, May 30, 2015

What is the SQL Server 2016 Query Data Store?

Query Data Store in SQL Server 2016 is intended as a service that monitors and optimizes query plans with full history of query execution. Query Data Store can help diagnose performance problems that are related to plan changes.
What it does:
  1. Stores the history of query plans
  2. Captures performance data for each query plan
  3. Identifies the most expensive queries and earmarks those queries that have degraded or gotten slower over time
  4. Gives you the ability to force old plans, should a new plan cause performance issues (almost like a system restore for Windows computers)
  5. Easily fixes plan regressions
  6. Analyzes workload patterns
  7. De-risks SQL Server upgrades with restart and failover

Read more http://windowsitpro.com/sql-server-2016/what-sql-server-2016-query-data-store

Tuesday, July 22, 2014

Difference Between LEN and DATALENGTH

DATALENGTH - Returns the number of bytes used to represent any expression.

LEN - Returns the number of characters of the specified string expression, excluding trailing blanks

image

Output is 30, 3.

Important point to remember is is LEN function does not depend on the data type and it is storage mechanism. LEN function total depends on the number of string characters.

image

Output is 8 and 3.

So DATALENGTH is depends on the storage but not the LEN function.

Tuesday, June 11, 2013

Avoiding Parameter Sniffing in SQL Server

Parameter sniffing is when SQL Server compiles a stored procedure’s execution plan with the first parameter that has been used and then uses this plan for subsequent executions regardless of the parameters.  Read more about parameter sniffing and how to avoid it from here.

Monday, May 20, 2013

Before Triggers in SQL Server

There are lots of questions asking whether there are before triggers in SQL Server. There are nothing called Before Triggers in SQL Server.

What is the requirement for the Before Trigger?

Let us say, you want to verify some values other table before inserting it. In SQL Server, you can use INSTEAD OF TRIGGER.

CREATE TRIGGER tr_data_before ON Table_Data
INSTEAD OF INSERT
AS
BEGIN
SET NOCOUNT ON
IF
EXISTS (SELECT 1 FROM Tablle_Data2)
BEGIN
INSERT INTO
dbo.Table_Data
SELECT *
FROM INSERTED
END
END

However, in Oracle and DB2 there are Before Triggers.

Sunday, May 19, 2013

What Operation Type Invoked a Trigger?

This was a question brought to my attention and I didn’t have an answer. After searching, I realized that there is no direct function for it. Hence I came up with following solution.

I used inserted and deleted virtual tables.

Statement

inserted

deleted

INSERT

rows just inserted

 

DELETE

 

rows just deleted

UPDATE

modified row contents

original row contents

With the above cases, I came up with following trigger.

CREATE TRIGGER trg_data_ins_del_upd
ON Data
FOR INSERT,DELETE,UPDATE
AS
BEGIN
DECLARE
@ins int ,@del int

SELECT
@ins = Count (*) From inserted
SELECT @del = Count (*) From deleted
IF @ins > 0 AND @del > 0
INSERT INTO Operation (Operation) VALUES ('Update')
ELSE IF @ins > 0
INSERT INTO Operation (Operation) VALUES ('Insert')
ELSE IF @del > 0
INSERT INTO Operation (Operation) VALUES ('Delete')
END

However, I feel in the above case, you better of having three separate triggers than loading everything to one trigger.

Monday, April 22, 2013

Reduce Dev Time and Improve Performance with MongoDB Training

Learning best practices for MongoDB development and administration can help reduce development time for your application, and increase reliability and performance. Get yourself and your team up to speed quickly with 10gen's comprehensive courses in person and online:

  • MongoDB for Developers: Learn best practices for document-based data moderling, queries and commands, map/reduce and basic administration.
  • MongoDB for Administrators: Diagnose performance issues, import and export data from MongoDB and establish the proper backup and restore routines.

Saturday, April 6, 2013

BIT Column Indexes

There are two myths around BIT column indexes.

Myth #1. Cannot create indexes on BIT columns.

Not sure how this was evolved. But this not absolutely false. Let us try it.

USE tempdb
GO

CREATE TABLE
Employee
(ID INT IDENTITY PRIMARY KEY CLUSTERED,
Name Varchar(50),
Status BIT
)

CREATE INDEX idx_employee_status ON Employee(Status)

Above statement is successful and you can create the index is created.


image


I verified this in SQL Server 2000 SP4 and it was successful.

Myth #2. BIT Column indexes are not useful.


Above argument has a base. BIT column can have possible three values, 0, 1 and NULL. Therefore index selectivity is low.  For example, consider the above table which has 10 million rows and status will say whether these are active or not.  In large table, active records will be very less so active = 0X1 will be highly selective.