1

I am struggling a bit to improve performance on one of my queries.

What I had is basically all of those 4 tables for reporting without any indexes, PKs, etc.

I was thinking that if I copy them (even on the same db) for testing purposes, and add indexes, PKs, etc., that I should see a big performance boost - which didn't happen.

The queries on both versions of the tables (with and without PKs and indexes) are the same - like, exactly the same. I have nonclustered indexes on the join columns, and on my main table (test_workstream) I have a primary key.

The query below produces 300k rows in 10 mins when run in SSMS.

I don't have much experience and would appreciate any tips.

  SELECT [workstream].[w_active] AS [w_active],
  [workstream].[w_name] AS [w_name],
  [workstream].[w_type] AS [w_type],
  [workstream].[w_reference] AS [w_reference],
  [workstream].[w_lead] AS [w_lead],
  [workstream].[w_completeness] AS [w_completeness],
  [workstream].[w_target] AS [w_target],
  [workstream].[w_startdate] AS [w_startdate],
  [workstream].[w_enddate] AS [w_enddate],
  [workstream].[w_itemtype] AS [w_itemtype],
  [workstream].[w_path] AS [w_path],
  [workstream].[ID] AS [ID],
  [workstream].[w_reporting] AS [w_reporting],
  [plan].[ID] AS [ID (plan)],
  [plan].[p_completeness] AS [p_completeness],
  [plan].[p_status] AS [p_status],
  [plan].[p_workstream] AS [p_workstream],
  [plan].[p_main_workstream] AS [p_main_workstream],
  [plan].[p_task_type] AS [p_task_type],
  [plan].[p_task_name] AS [p_task_name],
  [plan].[p_assignee] AS [p_assignee],
  [plan].[p_effort_work] AS [p_effort_work],
  [plan].[p_duration] AS [p_duration],
  [plan].[p_start_date] AS [p_start_date],
  [plan].[p_end_date] AS [p_end_date],
  [plan].[p_workpackage] AS [p_workpackage],
  [plan].[p_mandatory_phase] AS [p_mandatory_phase],
  [plan].[p_deliverable_link] AS [p_deliverable_link],
  [status_report].[sr_reporting_date] AS [sr_reporting_date],
  [status_report].[sr_reportingweek] AS [sr_reportingweek],
  [status_report].[sr_reportingyear] AS [sr_reportingyear],
  [status_report].[sr_status] AS [sr_status],
  [status_report].[sr_workstream] AS [sr_workstream],
  [status_report].[sr_workstream_lead] AS [sr_workstream_lead],
  [status_report].[sr_overall_previous] AS [sr_overall_previous],
  [status_report].[sr_overall_actual] AS [sr_overall_actual],
  [status_report].[sr_scope_previous] AS [sr_scope_previous],
  [status_report].[sr_scope_actual] AS [sr_scope_actual],
  [status_report].[sr_time_previous] AS [sr_time_previous],
  [status_report].[sr_time_actual] AS [sr_time_actual],
  [status_report].[sr_team_previous] AS [sr_team_previous],
  [status_report].[sr_team_actual] AS [sr_team_actual],
  [status_report].[sr_quality_previous] AS [sr_quality_previous],
  [status_report].[sr_quality_actual] AS [sr_quality_actual],
  [status_report].[sr_risk_previous] AS [sr_risk_previous],
  [status_report].[sr_risk_actual] AS [sr_risk_actual],
  [status_report].[sr_key_risk] AS [sr_key_risk],
  [status_report].[sr_working_results] AS [sr_working_results],
  [status_report].[sr_key_milestone] AS [sr_key_milestone],
  [status_report].[sr_up_task] AS [sr_up_task],
  [status_report].[sr_other_topics] AS [sr_other_topics],
  [status_report].[sr_last_update] AS [sr_last_update],
  [status_report].[sr_completeness_actual] AS [sr_completeness_actual],
  [status_report].[sr_completeness_previous] AS [sr_completeness_previous],
  [status_report].[ID] AS [ID (status_report)],
  [risk].[r_id] AS [r_id],
  [risk].[r_category] AS [r_category],
  [risk].[r_subcategory] AS [r_subcategory],
  [risk].[r_title] AS [r_title],
  [risk].[r_response] AS [r_response],
  [risk].[r_response_strategy] AS [r_response_strategy],

  [risk].[r_comment] AS [r_comment],
  [risk].[r_submitter] AS [r_submitter],
  [risk].[r_owner] AS [r_owner],
  [risk].[r_datecreated] AS [r_datecreated],
  [risk].[r_status] AS [r_status],
  [risk].[r_assigned] AS [r_assigned],
  [risk].[r_target_resolution] AS [r_target_resolution],
  [risk].[r_last_review] AS [r_last_review],
  [risk].[r_risk_impact] AS [r_risk_impact],
  [risk].[r_risk_probability] AS [r_risk_probability],
  [risk].[r_risk_rating] AS [r_risk_rating],
  [risk].[r_review_impact] AS [r_review_impact],
  [risk].[r_review_probability] AS [r_review_probability],
  [risk].[r_review_rating] AS [r_review_rating],
  [risk].[ID] AS [ID (risk)]


FROM [dbo].[test_workstream] [workstream]
   left JOIN [dbo].[test_status_report] [status_report] ON ([workstream].id = [status_report].sr_workstream)
   left JOIN [dbo].[test_risk] [risk] ON ([workstream].id = [risk].worksteam_id)
   left JOIN [dbo].[test_plan] [plan] ON ([workstream].id = [plan].[p_workstream])

enter image description here

DDL :

USE [master]
GO
/****** Object:  Database [PMO_NEXUS_ERP_TEST]    Script Date: 17.07.2019 21:14:56 ******/
CREATE DATABASE [PMO_NEXUS_ERP_TEST]
 CONTAINMENT = NONE
 ON  PRIMARY 
( NAME = N'PMO_NEXUS_ERP', FILENAME = N'H:\SQL2016\MSSQL13.BASSQL2K16D1\MSSQL\DATA\PMO_NEXUS_ERP_TEST.mdf' , SIZE = 1048576KB , MAXSIZE = UNLIMITED, FILEGROWTH = 524288KB )
 LOG ON 
( NAME = N'PMO_NEXUS_ERP_Log', FILENAME = N'I:\SQL2016\MSSQL13.BASSQL2K16D1\MSSQL\Data\PMO_NEXUS_ERP_TEST_Log.ldf' , SIZE = 524288KB , MAXSIZE = 2048GB , FILEGROWTH = 262144KB )
GO
ALTER DATABASE [PMO_NEXUS_ERP_TEST] SET COMPATIBILITY_LEVEL = 130
GO
IF (1 = FULLTEXTSERVICEPROPERTY('IsFullTextInstalled'))
begin
EXEC [PMO_NEXUS_ERP_TEST].[dbo].[sp_fulltext_database] @action = 'enable'
end
GO
ALTER DATABASE [PMO_NEXUS_ERP_TEST] SET ANSI_NULL_DEFAULT OFF 
GO
ALTER DATABASE [PMO_NEXUS_ERP_TEST] SET ANSI_NULLS OFF 
GO
ALTER DATABASE [PMO_NEXUS_ERP_TEST] SET ANSI_PADDING OFF 
GO
ALTER DATABASE [PMO_NEXUS_ERP_TEST] SET ANSI_WARNINGS OFF 
GO
ALTER DATABASE [PMO_NEXUS_ERP_TEST] SET ARITHABORT OFF 
GO
ALTER DATABASE [PMO_NEXUS_ERP_TEST] SET AUTO_CLOSE OFF 
GO
ALTER DATABASE [PMO_NEXUS_ERP_TEST] SET AUTO_SHRINK OFF 
GO
ALTER DATABASE [PMO_NEXUS_ERP_TEST] SET AUTO_UPDATE_STATISTICS ON 
GO
ALTER DATABASE [PMO_NEXUS_ERP_TEST] SET CURSOR_CLOSE_ON_COMMIT OFF 
GO
ALTER DATABASE [PMO_NEXUS_ERP_TEST] SET CURSOR_DEFAULT  GLOBAL 
GO
ALTER DATABASE [PMO_NEXUS_ERP_TEST] SET CONCAT_NULL_YIELDS_NULL OFF 
GO
ALTER DATABASE [PMO_NEXUS_ERP_TEST] SET NUMERIC_ROUNDABORT OFF 
GO
ALTER DATABASE [PMO_NEXUS_ERP_TEST] SET QUOTED_IDENTIFIER OFF 
GO
ALTER DATABASE [PMO_NEXUS_ERP_TEST] SET RECURSIVE_TRIGGERS OFF 
GO
ALTER DATABASE [PMO_NEXUS_ERP_TEST] SET  DISABLE_BROKER 
GO
ALTER DATABASE [PMO_NEXUS_ERP_TEST] SET AUTO_UPDATE_STATISTICS_ASYNC OFF 
GO
ALTER DATABASE [PMO_NEXUS_ERP_TEST] SET DATE_CORRELATION_OPTIMIZATION OFF 
GO
ALTER DATABASE [PMO_NEXUS_ERP_TEST] SET TRUSTWORTHY OFF 
GO
ALTER DATABASE [PMO_NEXUS_ERP_TEST] SET ALLOW_SNAPSHOT_ISOLATION OFF 
GO
ALTER DATABASE [PMO_NEXUS_ERP_TEST] SET PARAMETERIZATION SIMPLE 
GO
ALTER DATABASE [PMO_NEXUS_ERP_TEST] SET READ_COMMITTED_SNAPSHOT OFF 
GO
ALTER DATABASE [PMO_NEXUS_ERP_TEST] SET HONOR_BROKER_PRIORITY OFF 
GO
ALTER DATABASE [PMO_NEXUS_ERP_TEST] SET RECOVERY SIMPLE 
GO
ALTER DATABASE [PMO_NEXUS_ERP_TEST] SET  MULTI_USER 
GO
ALTER DATABASE [PMO_NEXUS_ERP_TEST] SET PAGE_VERIFY CHECKSUM  
GO
ALTER DATABASE [PMO_NEXUS_ERP_TEST] SET DB_CHAINING OFF 
GO
ALTER DATABASE [PMO_NEXUS_ERP_TEST] SET FILESTREAM( NON_TRANSACTED_ACCESS = OFF ) 
GO
ALTER DATABASE [PMO_NEXUS_ERP_TEST] SET TARGET_RECOVERY_TIME = 60 SECONDS 
GO
ALTER DATABASE [PMO_NEXUS_ERP_TEST] SET DELAYED_DURABILITY = DISABLED 
GO
EXEC sys.sp_db_vardecimal_storage_format N'PMO_NEXUS_ERP_TEST', N'ON'
GO
ALTER DATABASE [PMO_NEXUS_ERP_TEST] SET QUERY_STORE = OFF
GO
USE [PMO_NEXUS_ERP_TEST]
GO
ALTER DATABASE SCOPED CONFIGURATION SET LEGACY_CARDINALITY_ESTIMATION = OFF;
GO
ALTER DATABASE SCOPED CONFIGURATION SET MAXDOP = 0;
GO
ALTER DATABASE SCOPED CONFIGURATION SET PARAMETER_SNIFFING = ON;
GO
ALTER DATABASE SCOPED CONFIGURATION SET QUERY_OPTIMIZER_HOTFIXES = OFF;
GO
USE [PMO_NEXUS_ERP_TEST]
GO

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[test_plan](
    [ID] [int] IDENTITY(1,1) NOT NULL,
    [p_completeness] [float] NOT NULL,
    [p_status] [varchar](20) NOT NULL,
    [p_workstream] [int] NOT NULL,
    [p_main_workstream] [varchar](400) NOT NULL,
    [p_task_type] [varchar](200) NOT NULL,
    [p_task_name] [varchar](500) NOT NULL,
    [p_assignee] [varchar](1000) NOT NULL,
    [p_effort_work] [float] NULL,
    [p_duration] [float] NOT NULL,
    [p_start_date] [date] NULL,
    [p_end_date] [date] NULL,
    [p_workpackage] [varchar](500) NOT NULL,
    [p_mandatory_phase] [nvarchar](50) NULL,
    [p_deliverable_link] [varchar](500) NULL
) ON [PRIMARY]
GO
/****** Object:  Table [dbo].[test_risk]    Script Date: 17.07.2019 21:14:57 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[test_risk](
    [r_id] [varchar](10) NOT NULL,
    [r_category] [varchar](200) NULL,
    [r_subcategory] [varchar](200) NULL,
    [r_title] [varchar](800) NOT NULL,
    [r_response] [varchar](50) NOT NULL,
    [r_response_strategy] [varchar](max) NOT NULL,
    [worksteam_id] [int] NOT NULL,
    [r_comment] [varchar](max) NOT NULL,
    [r_submitter] [varchar](200) NOT NULL,
    [r_owner] [varchar](200) NOT NULL,
    [r_datecreated] [date] NOT NULL,
    [r_status] [varchar](50) NOT NULL,
    [r_assigned] [varchar](200) NOT NULL,
    [r_target_resolution] [date] NOT NULL,
    [r_last_review] [date] NOT NULL,
    [r_risk_impact] [int] NOT NULL,
    [r_risk_probability] [int] NOT NULL,
    [r_risk_rating] [int] NOT NULL,
    [r_review_impact] [int] NOT NULL,
    [r_review_probability] [int] NOT NULL,
    [r_review_rating] [int] NOT NULL,
    [ID] [int] IDENTITY(1,1) NOT NULL,
 CONSTRAINT [PK_test_risk] PRIMARY KEY CLUSTERED 
(
    [ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
/****** Object:  Table [dbo].[test_status_report]    Script Date: 17.07.2019 21:14:57 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[test_status_report](
    [sr_reporting_date] [varchar](200) NOT NULL,
    [sr_reportingweek] [int] NOT NULL,
    [sr_reportingyear] [int] NOT NULL,
    [sr_status] [varchar](50) NOT NULL,
    [sr_workstream] [int] NOT NULL,
    [sr_workstream_lead] [varchar](100) NOT NULL,
    [sr_overall_previous] [varchar](20) NOT NULL,
    [sr_overall_actual] [varchar](20) NOT NULL,
    [sr_scope_previous] [varchar](20) NOT NULL,
    [sr_scope_actual] [varchar](20) NOT NULL,
    [sr_time_previous] [varchar](20) NOT NULL,
    [sr_time_actual] [varchar](20) NOT NULL,
    [sr_team_previous] [varchar](20) NOT NULL,
    [sr_team_actual] [varchar](20) NOT NULL,
    [sr_quality_previous] [varchar](20) NOT NULL,
    [sr_quality_actual] [varchar](20) NOT NULL,
    [sr_risk_previous] [varchar](20) NOT NULL,
    [sr_risk_actual] [varchar](20) NOT NULL,
    [sr_key_risk] [varchar](max) NOT NULL,
    [sr_working_results] [varchar](max) NOT NULL,
    [sr_key_milestone] [varchar](max) NOT NULL,
    [sr_up_task] [varchar](max) NOT NULL,
    [sr_other_topics] [varchar](max) NOT NULL,
    [sr_last_update] [varchar](10) NOT NULL,
    [sr_completeness_actual] [numeric](10, 2) NOT NULL,
    [sr_completeness_previous] [numeric](10, 2) NOT NULL,
    [ID] [int] IDENTITY(1,1) NOT NULL,
 CONSTRAINT [PK_test_status_report] PRIMARY KEY CLUSTERED 
(
    [ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
/****** Object:  Table [dbo].[test_workstream]    Script Date: 17.07.2019 21:14:57 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[test_workstream](
    [w_active] [bit] NULL,
    [w_name] [varchar](200) NOT NULL,
    [w_type] [varchar](100) NOT NULL,
    [w_reference] [varchar](100) NOT NULL,
    [w_lead] [varchar](200) NOT NULL,
    [w_completeness] [float] NULL,
    [w_target] [float] NULL,
    [w_startdate] [date] NULL,
    [w_enddate] [date] NULL,
    [w_itemtype] [nvarchar](50) NULL,
    [w_path] [nvarchar](max) NULL,
    [ID] [int] IDENTITY(1,1) NOT NULL,
    [w_reporting] [bit] NULL,
 CONSTRAINT [PK_test_workstream] PRIMARY KEY CLUSTERED 
(
    [ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]


GO
/****** Object:  Index [ix_testplan_id_worksteam]    Script Date: 17.07.2019 21:14:57 ******/
CREATE NONCLUSTERED INDEX [ix_testplan_id_worksteam] ON [dbo].[test_plan]
(
    [p_workstream] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
/****** Object:  Index [IX_r_worksteam]    Script Date: 17.07.2019 21:14:57 ******/
CREATE NONCLUSTERED INDEX [IX_r_worksteam] ON [dbo].[test_risk]
(
    [worksteam_id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
/****** Object:  Index [ix_teststatusreport_id_worksteam]    Script Date: 17.07.2019 21:14:57 ******/
CREATE NONCLUSTERED INDEX [ix_teststatusreport_id_worksteam] ON [dbo].[test_status_report]
(
    [sr_workstream] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
ALTER TABLE [dbo].[test_plan] ADD  CONSTRAINT [test_DF_plan_p_mandatory_phase]  DEFAULT (N'not mandatory') FOR [p_mandatory_phase]
GO
ALTER TABLE [dbo].[test_risk] ADD  CONSTRAINT [test_DF_risk_r_risk_impact]  DEFAULT ((0)) FOR [r_risk_impact]
GO
ALTER TABLE [dbo].[test_risk] ADD  CONSTRAINT [test_DF_risk_r_risk_probability]  DEFAULT ((0)) FOR [r_risk_probability]
GO
ALTER TABLE [dbo].[test_risk] ADD  CONSTRAINT [test_DF_risk_r_risk_rating]  DEFAULT ((0)) FOR [r_risk_rating]
GO
ALTER TABLE [dbo].[test_status_report] ADD  CONSTRAINT [test_DF_status_report_sr_completeness_act]  DEFAULT ((0)) FOR [sr_completeness_actual]
GO
ALTER TABLE [dbo].[test_status_report] ADD  CONSTRAINT [test_DF_status_report_sr_completeness_previous]  DEFAULT ((0)) FOR [sr_completeness_previous]
GO
ALTER TABLE [dbo].[test_workstream] ADD  CONSTRAINT [test_DF_workstream_w_active]  DEFAULT ((1)) FOR [w_active]
GO
ALTER TABLE [dbo].[test_workstream] ADD  CONSTRAINT [test_DF_workstream_w_completeness_active]  DEFAULT ((0)) FOR [w_completeness]
GO
ALTER TABLE [dbo].[test_workstream] ADD  CONSTRAINT [test_DF_workstream_w_reporting]  DEFAULT ((1)) FOR [w_reporting]


GO
ALTER TABLE [dbo].[test_risk]  WITH CHECK ADD  CONSTRAINT [FK_test_risk_test_workstream] FOREIGN KEY([worksteam_id])
REFERENCES [dbo].[test_workstream] ([ID])
GO
ALTER TABLE [dbo].[test_risk] CHECK CONSTRAINT [FK_test_risk_test_workstream]
GO
ALTER TABLE [dbo].[test_status_report]  WITH CHECK ADD  CONSTRAINT [FK_test_status_report_test_workstream] FOREIGN KEY([sr_workstream])
REFERENCES [dbo].[test_workstream] ([ID])
GO
ALTER TABLE [dbo].[test_status_report] CHECK CONSTRAINT [FK_test_status_report_test_workstream]
GO
USE [master]
GO
ALTER DATABASE [PMO_NEXUS_ERP_TEST] SET  READ_WRITE 
GO
4

Per the operator times in your screenshot (which can be...uh...confusing), your query finishes in about 1.2 seconds.

You mentioned that the query took ~10 minutes to finish in SSMS. This difference is mainly the time it takes for SSMS to consume and render the results to the grid.

As evidence for that, you can check out the "wait stats" in the execution plan. I imagine you'll see quite a bit of ASYNC_NETWORK_IO. You can see that by right-clicking on the SELECT node and choosing "properties," then expanding the "WaitStats" node in the properties pane. It looks like this:

screenshot of waitstats node in SSMS

Overall, your query seems reasonably fast when it's not being rendered by SSMS. I could give you some generic advice (put a clustered index test_plan table unless there's a really good reason to leave it as a heap, add indexes to avoid those sorts, etc), but it's hard to know if any of that is really necessary.

I'd hold off on tuning this more until you start having a problem with it, or row counts start rising quite a bit.


By the way, if you just want a better way to test this query in SSMS without rendering all the rows, you can turn off result sets by going to the "Query" menu at the top of SSMS, then "Query Options" -> "Results" -> "Grid" -> and check the box for "Discard results after execution."

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.