http://geekswithblogs.net/EltonStoneman/archive/2014/01/05/the-concurrency-shoot-out-sql-server-versus-mongodb-part-3.aspx
I couldn't post a proper response to the above link so I'm posting here and will post the link instead.
It took me a while before I stumbled back on this again. Plenty of tuning could be done on the SQL side. I've scripted up the most obvious below as there were some definite things that will improve performance. To get more out of it would need a little longer than I have, but there is plenty more to come out when you dig into what is actually being queried.
This is the pain in the arse with SQL if I'm honest. It's not obvious and doesn't tell you where you can improve or how. It'll allow you to do a lot (badly) but if you know what you are doing you can make it fly.
Always worth watching http://sqlbits.com/Sessions/Event9/Finding_the_Limits to see how much you can push SQL.
Anyway, some basic tweaks below.
USE master
GO
SET NOCOUNT ON
GO
sp_configure 'show advanced options',1
GO
RECONFIGURE WITH OVERRIDE
GO
sp_configure 'contained database authentication', 1
GO
RECONFIGURE WITH OVERRIDE
GO
IF NOT EXISTS (SELECT 1 FROM sys.databases WHERE name = 'MongoTest' )
BEGIN
BEGIN TRY
CREATE DATABASE
MongoTest CONTAINMENT = PARTIAL ON (NAME = MongoTest,
FILENAME = 'D:\MongoTest\MongoTest.mdf', SIZE = 1GB, FILEGROWTH = 256MB) --Made as small as possible simply for testing
LOG ON (NAME = MongoTest_log,
FILENAME = 'D:\MongoTest\MongoTest_log.ldf', SIZE = 1GB, FILEGROWTH = 256MB)
ALTER DATABASE MongoTest ADD FILEGROUP FG_STANDARD
PRINT 'Created FileGroup FG_Standard'
ALTER DATABASE MongoTest ADD FILE (NAME = Standard_01, FILENAME = 'D:\MongoTest\Standard_01.ndf', SIZE = 1GB, FILEGROWTH = 256MB) TO FILEGROUP FG_Standard
ALTER DATABASE MongoTest ADD FILE (NAME = Standard_02, FILENAME = 'D:\MongoTest\Standard_02.ndf', SIZE = 1GB, FILEGROWTH = 256MB) TO FILEGROUP FG_Standard
ALTER DATABASE MongoTest ADD FILE (NAME = Standard_03, FILENAME = 'D:\MongoTest\Standard_03.ndf', SIZE = 1GB, FILEGROWTH = 256MB) TO FILEGROUP FG_Standard
ALTER DATABASE MongoTest ADD FILE (NAME = Standard_04, FILENAME = 'D:\MongoTest\Standard_04.ndf', SIZE = 1GB, FILEGROWTH = 256MB) TO FILEGROUP FG_Standard
PRINT 'Created 4 files for FG_Standard'
--Multiple files to help performance. Worth googling.
ALTER DATABASE MongoTest MODIFY FILEGROUP FG_Standard DEFAULT
PRINT 'Database created: MongoTest'
END TRY
BEGIN CATCH
PRINT '*** Database not created: MongoTest ***'
END CATCH
END
ELSE
BEGIN
PRINT '*** Database already created: MongoTest ***'
END
GO
USE MongoTest
GO
EXEC sys.sp_changedbowner 'sa'
GO
PRINT 'Database owner for MongoTest changed to sa'
GO
USE MongoTest
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS(SELECT 1 FROM INFORMATION_SCHEMA.Tables WHERE TABLE_SCHEMA = 'dbo' AND TABLE_NAME = 'ParentEntities')
BEGIN
CREATE TABLE dbo.ParentEntities (
Id INT IDENTITY(1, 1) NOT NULL,
CorrelationId NVARCHAR(50) NULL, --No idea where you are using this column
Name NVARCHAR(255) NULL,
CONSTRAINT PKN_ParentEntities PRIMARY KEY NONCLUSTERED (Id ASC)) --Again
ON FG_Standard
WITH (DATA_COMPRESSION = PAGE)
CREATE UNIQUE CLUSTERED INDEX CIDX_ParentEntities --Again
ON dbo.EntityEvents (Name ASC)
WITH (DATA_COMPRESSION = PAGE) --Optional if on Enterprise Edition
ON FG_Standard --Using different files for recovery reasons, my standard approach to scripting
PRINT 'Created Table dbo.ParentEntities'
END
ELSE
BEGIN
PRINT '*** Table dbo.ParentEntities already exists ***'
END
GO
--If you query by anything other than 'Name' in the above then you need to check the execution plan and build the query and index accordingly.
IF NOT EXISTS(SELECT 1 FROM INFORMATION_SCHEMA.Tables WHERE TABLE_SCHEMA = 'dbo' AND TABLE_NAME = 'Entities')
BEGIN
CREATE TABLE dbo.Entities ( --Table names shouldn't be plural
Id INT IDENTITY(1, 1) NOT NULL, --Ideally a more descriptive name would be better.
Name NVARCHAR(255) NULL, --Ideally you wouldn't use reserved words here but hey.
ParentId INT NOT NULL,
StatusCode INT NOT NULL,
ParentEntity_Id INT NULL,
CONSTRAINT PKN_Entities PRIMARY KEY NONCLUSTERED (MarketId ASC)) --Having the ID as a Clustered Index is likely pointless here.
ON FG_Standard
WITH (DATA_COMPRESSION = PAGE)
CREATE UNIQUE CLUSTERED INDEX CIDX_Entities --Assuming you are most likely searching by name it makes more sense to have this as the clustered index
-- so that we do a single pass through the b-tree index. The previous version lacked proper indexing.
ON dbo.Entities (Name ASC)
WITH (DATA_COMPRESSION = PAGE) --Optional if on Enterprise Edition
ON FG_Standard --Using different files for recovery reasons, my standard approach to scripting
ALTER TABLE dbo.Entities
ADD CONSTRAINT FK_Entities_ParentEntity_Id
FOREIGN KEY (ParentEntity_Id)
REFERENCES dbo.ParentEntities (Id)
PRINT 'Created Table dbo.Entities'
END
ELSE
BEGIN
PRINT '*** Table dbo.Entities already exists ***'
END
GO
--If you query by anything other than 'Name' in the above then you need to check the execution plan and build the query and index accordingly.
IF NOT EXISTS(SELECT 1 FROM INFORMATION_SCHEMA.Tables WHERE TABLE_SCHEMA = 'dbo' AND TABLE_NAME = 'EntityEvents')
BEGIN
CREATE TABLE dbo.EntityEvents (
Id int IDENTITY(1, 1) NOT NULL, --again, better naming convention would help
[Description] NVARCHAR(255) NULL, --as above
EntityId INT NOT NULL, --In the first table this should also be called the same as it is so much easier to follow
CreatedAt DATETIME2 NOT NULL, --Changed to DateTime2 which is the better approach these days
StatusCode INT NOT NULL,
CONSTRAINT PKN_EntityEvents PRIMARY KEY NONCLUSTERED (Id ASC)) --Again
ON FG_Standard
WITH (DATA_COMPRESSION = PAGE)
CREATE UNIQUE CLUSTERED INDEX CIDX_EntityEvents --Again...
ON dbo.EntityEvents ([Description] ASC)
WITH (DATA_COMPRESSION = PAGE) --Again
ON FG_Standard --Again
ALTER TABLE dbo.EntityEvents
ADD CONSTRAINT FK_EntityEvents_EntityId
FOREIGN KEY (EntityId)
REFERENCES dbo.Entities (Id)
PRINT 'Created Table dbo.EntityEvents'
END
ELSE
BEGIN
PRINT '*** Table dbo.EntityEvents already exists ***'
END
GO
--If you query by anything other than 'Description' in the above then you need to check the execution plan and build the query and index accordingly.
--Things NOT looked at (not exhaustive)
--Partitioning - Enterprise only feature
--Execution plans - these are key to understanding the data
--Selectivity/Density
--Fill Factor (Mongo uses the term index padding but its the same thing effectively)
--Query Plan re-use - very important if you are using an ORM such as nHibernate
--How many files are actually needed for the DB
--What queries are to be run. I didn't dig into the code, purely the schema above
--What delays we can eradicate when this is run under load. This takes a little longer but is always worth doing (wait stats etc...)
--Locking, Blocking and Transaction Isolation level etc... Lets fix the basics first
--Any spills to Temp DB. This always kills performance.
--Whether or not the data resides in memory. If it doesn't on SQL then the comparison is really memory vs disk.
--There is likely plenty more. I gave this about 10 mins.
/*Rollback*/
/*
DROP TABLE dbo.EntityEvents
DROP TABLE dbo.Entities
DROP TABLE dbo.ParentEntities
*/
Subscribe to:
Post Comments (Atom)
CV Driven Development
Over recent years I've watched a worrying new trend emerge which I think of as CV Driven Development. What I mean by this is that people...
-
I've been looking at a few things with MongoDB recently with some of the guys at work. It was brought to me as a 'Big Data Problem...
-
http://geekswithblogs.net/EltonStoneman/archive/2014/01/05/the-concurrency-shoot-out-sql-server-versus-mongodb-part-3.aspx I couldn't ...
I suspect (intuition!) that the majority of SQL Server databases aren't tuned using anything like what you've suggested, so a good test would be to use settings that are as close to the defaults as possible, because this will likely reflect real world setups. The original article - and its source code - indeed confirms that this is the case.
ReplyDeleteI highly suspect, too, that the MongoDB performance could be further tuned.