DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workkloads.

Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Unlocking the Power of Configuration Management Database (CMDB)
  • Data Store Options for Operational Analytics/Data Engineering
  • Common Mistakes to Avoid When Writing SQL Code
  • An Overview of SQL Server Joins

Trending

  • Recurrent Workflows With Cloud Native Dapr Jobs
  • Beyond Linguistics: Real-Time Domain Event Mapping with WebSocket and Spring Boot
  • Streamlining Event Data in Event-Driven Ansible
  • Mastering Fluent Bit: Installing and Configuring Fluent Bit on Kubernetes (Part 3)
  1. DZone
  2. Data Engineering
  3. Databases
  4. Creating an Index within a Stored Procedure

Creating an Index within a Stored Procedure

Is it better to create an index with a stored procedure? Well, you can do it -- but this solution causes another problem. Here's how to solve both at once.

By 
Joydeep Das user avatar
Joydeep Das
·
Jul. 24, 15 · Tutorial
Likes (0)
Comment
Save
Tweet
Share
35.8K Views

Join the DZone community and get the full member experience.

Join For Free

Introduction

One of my friends asked: "Is it better to create an Index within a Stored Procedure?" While it depends on the columns used in a stored procedure to improve the Join performance and Drop the stored procedure after getting the desired output and just before the ending...

Technical Answer

...yes, we can create an Index within the Stored Procedure.

Now, We Have to Think What Our Answer means

Technically, what my friend mentioned in the scenario is possible. But performance wise, it’s again a problematic solution. That’s means to solve something we again create another problem. To understand it properly let’s take an example of such kind of Stored Procedure.

 IF OBJECT_ID(N'[dbo].[sproc_StudentDetails]', N'P')IS NOT NULL
   DROP PROCEDURE [dbo].[sproc_StudentDetails];
GO

CREATE PROCEDURE [dbo].[sproc_StudentDetails]
     (
         @p_StdClass   INT
      )
AS
BEGIN
      -- Creating Non Clustered Index on IDNO
      CREATE NONCLUSTERED INDEX IX_NC_IDNO_StdDtl
              ON tbl_StudentDetails(IDNO);

       CREATE NONCLUSTERED INDEX IX_NC_IDNO_StdMarks
              ON tbl_StudentMarks(IDNO);

      -- Making JOIN on IDNO for Performance Reason
       SELECT a.Roll, a.StdName, b.Marks1, b.Marks2
       FROM   tbl_StudentDetails AS a
              INNER JOIN tbl_StudentMarks AS b ON a.IDNO = b.IDNO;

      -- Droping the Index
       DROP INDEX IX_NC_IDNO_StdDtl ON tbl_StudentDetails;
       DROP INDEX IX_NC_IDNO_StdMarks ON tbl_StudentMarks;
END
GO

Here in this example if we look carefully the No clustered Index is created and after successful joining it again drops.

It is technically perfect.

So, What Is the Problem...

 SELECT a.Roll, a.StdName, b.Marks1, b.Marks2
       FROM   tbl_StudentDetails AS a
              INNER JOIN tbl_StudentMarks AS b ON a.IDNO = b.IDNO;

The performance of the JOIN condition is increased due to a non clustered index. So we think that we got the performance gain...but, in fact, we did NOT.  Always understand fully before implementing.

When the Index created the Index table and the Statistical table, both are updated, so making an index within a stored procedure again takes a long time to create.

By the index creation we solve the Join performance, but Index creation time is again a pain point and slows down the performance of a stored procedure.

Hope you like it.


Database Joins (concurrency library) Drops (app) clustered IT

Published at DZone with permission of Joydeep Das, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Unlocking the Power of Configuration Management Database (CMDB)
  • Data Store Options for Operational Analytics/Data Engineering
  • Common Mistakes to Avoid When Writing SQL Code
  • An Overview of SQL Server Joins

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!