SlideShare uma empresa Scribd logo
1 de 18
Performance
Considerations –
Dapper .Net
Agenda
• Query Caching
• Buffering
• Query Vs QueryMultiple
• Dirty Tracking
• Q & A
Query Caching
• Dapper caches information about every query it runs, this
allow it to materialize objects quickly and process parameters
quickly.
• The current implementation caches this information in a
ConcurrentDictionary object.
static readonly ConcurrentDictionary<Identity, CacheInfo> _queryCache =
new ConcurrentDictionary<Identity, CacheInfo>();
• The objects it stores are never flushed.
• If you are generating SQL strings on the fly without using
parameters it is possible you will hit memory issues.
• Each query you issue will create an Identity, depending on the
SQL query, its command type and its parameters.
• The CacheInfo object contains IDataReader and IDBCommand
functions and some counters which limit the cached amount.
Cont…
• The Identity class which is used for caching will look like as
mentioned in the below slide,
private Identity(string sql, CommandType? commandType, string connectionString, Type type, Type
parametersType, Type[] otherTypes, int gridIndex)
{
this.sql = sql;
this.commandType = commandType;
this.connectionString = connectionString;
this.type = type;
this.parametersType = parametersType;
this.gridIndex = gridIndex;
unchecked
{
hashCode = 17; // we *know* we are using this in a dictionary, so pre-compute this
hashCode = hashCode * 23 + commandType.GetHashCode();
hashCode = hashCode * 23 + gridIndex.GetHashCode();
hashCode = hashCode * 23 + (sql == null ? 0 : sql.GetHashCode());
hashCode = hashCode * 23 + (type == null ? 0 : type.GetHashCode());
if (otherTypes != null)
{
foreach (var t in otherTypes)
{ hashCode = hashCode * 23 + (t == null ? 0 : t.GetHashCode());
}
}
hashCode = hashCode * 23 + (connectionString == null ? 0 : connectionString.GetHashCode());
hashCode = hashCode * 23 + (parametersType == null ? 0 : parametersType.GetHashCode());
}
}
CacheInfo Class
class CacheInfo
{
public Func<IDataReader, object> Deserializer { get; set; }
public Func<IDataReader, object>[] OtherDeserializers { get; set; }
public Action<IDbCommand, object> ParamReader { get; set; }
private int hitCount;
public int GetHitCount() { return Interlocked.CompareExchange(ref hitCount, 0, 0); }
public void RecordHit() { Interlocked.Increment(ref hitCount); }
}
Note on Caching
• Use this
string s = "SELECT email, passwd, login_id, full_name " + "FROM members WHERE " + "email =
@email";
SqlCommand cmd = new SqlCommand(s); cmd.Parameters.Add("@email", email);
• Instead of
cmd.CommandText = "SELECT email, passwd, login_id, full_name " + "FROM members " +
"WHERE email = '" + email + "'";
• The first one is parameterized. It will be cached once. The
second one is not parameterized. It will be cached every time
you write a query like it with a different value for email. This
will explode your memory. (+)
• The first one is vastly superior. It avoids injection attacks.
dapper can cache it once. SQL Server will compile the
execution plan once and cache it. (+)
Buffering
• The buffer is unrelated to cache.
• Dapper does not include any kind of data-cache (although it
does have a cache related to the way how it processes
commands, i.e. "this command string, with this type of
parameter, and this type of entity - has these associated
dynamically generated methods to configure the command
and populate the objects").
• It’s a bool value, supplied against each command object. By
default buffering set to true.
Buffer = true
• In a buffered API all the rows are read before anything is
yielded.
• When dealing with the limited no of rows for example 100 or
200. So that it consumes less memory.
• Once you get the data, the command is complete - so there is
no conflict between that and subsequent operations. (+)
• It doesn’t not hold the active connection for a long time. (+)
• As soon as you get the data, the command has already
released any resources (locks etc), so you're having minimal
impact on the server. (+)
• If the query is immense, loading them all into memory (in a
list) could be expensive / impossible. (-)
• High latency time. (-)
Buffer = false
• If at all dealing with large amount of data (thousands to
millions of rows). It consumes lot of memory for storing the
buffered data.
• you can iterate over immense queries (many millions of rows),
without needing them all in-memory at once - since you're
only ever really looking at the current row being yielded. (+)
• In a streaming API each element is yielded individually. This is
very memory efficient, but if you do lots of subsequent
processing per item, mean that your connection / command
could be "active" for an extended time. (+)
• You don't need to wait for the end of the data to start iterating
- as soon as it has at least one row. (+)
Cont…
• The connection is in-use while you're iterating, which can lead
to "there is already an open reader on the connection" (or
whatever the exact wording is) errors if you try to invoke other
commands on a per-row basis (this can be mitigated by
MARS). (-)
• It holds the active connection for long time, when deals with
large amount of data. (-)
Query Vs QueryMultiple
• We need to choose the right one whether to use Query or
QueryMultiple.
• Its completely based upon the no of resultsets expected from
the command. If expected is more than one resultset we must
use QueryMultiple. If not we must use Query.
• QueryMultiple has some more additional logic. It applies some
amount of complication.
QueryMultiple Example
var sql =
@"
select * from Customers where CustomerId = @id
select * from Orders where CustomerId = @id
select * from Returns where CustomerId = @id";
using (var multi = connection.QueryMultiple(sql, new { id = value }))
{
var customer = multi.Read<Customer>().Single();
var orders = multi.Read<Order>().ToList();
var returns = multi.Read<Return>().ToList();
}
Dirty Tracking
• Dapper .net provides a nice feature to determine if the update
statement is really required. If the value does not change, it
won’t generate the SQL statement, which is very handy
performance optimization.
• The only requirement is we need to declare a interface for the
object.
Update – Without Tracking
using (var sqlConnection = new
SqlConnection(Constant.DatabaseConnection))
{
sqlConnection.Open();
var entity = sqlConnection.Get(9);
entity.ContactName = "John Smith";
sqlConnection.Update(entity);
var result = sqlConnection.Get(9);
}
Update – With Tracking
public interface ISupplier
{
int Id { get; set; }
string CompanyName { get; set; }
string ContactName { get; set; }
string ContactTitle { get; set; }
}
public class Supplier : ISupplier
{
public int Id { get; set; }
public string CompanyName { get; set; }
public string ContactName { get; set; }
public string ContactTitle { get; set; }
}
Cont…
using (var sqlConnection = new SqlConnection(conString))
{
sqlConnection.Open();
var supplier = sqlConnection.Get(9);
Console.WriteLine(string.Format("IsUpdated {0}",
sqlConnection.Update(supplier)));
supplier.CompanyName = “NewManning";
Console.WriteLine(string.Format("IsUpdated {0}",
sqlConnection.Update(supplier)));
}
Q & A

Mais conteúdo relacionado

Mais procurados

Servlet and jsp interview questions
Servlet and jsp interview questionsServlet and jsp interview questions
Servlet and jsp interview questionsSujata Regoti
 
Java Persistence API (JPA) Step By Step
Java Persistence API (JPA) Step By StepJava Persistence API (JPA) Step By Step
Java Persistence API (JPA) Step By StepGuo Albert
 
PHP para Adultos: Clean Code e Object Calisthenics
PHP para Adultos: Clean Code e Object CalisthenicsPHP para Adultos: Clean Code e Object Calisthenics
PHP para Adultos: Clean Code e Object CalisthenicsGuilherme Blanco
 
PowerShell for Penetration Testers
PowerShell for Penetration TestersPowerShell for Penetration Testers
PowerShell for Penetration TestersNikhil Mittal
 
Performance Analysis: The USE Method
Performance Analysis: The USE MethodPerformance Analysis: The USE Method
Performance Analysis: The USE MethodBrendan Gregg
 
Keeping Spark on Track: Productionizing Spark for ETL
Keeping Spark on Track: Productionizing Spark for ETLKeeping Spark on Track: Productionizing Spark for ETL
Keeping Spark on Track: Productionizing Spark for ETLDatabricks
 
Heap Dump Analysis - AEM: Real World Issues
Heap Dump Analysis - AEM: Real World IssuesHeap Dump Analysis - AEM: Real World Issues
Heap Dump Analysis - AEM: Real World IssuesKanika Gera
 
Outlook and Exchange for the bad guys
Outlook and Exchange for the bad guysOutlook and Exchange for the bad guys
Outlook and Exchange for the bad guysNick Landers
 
Jpa 잘 (하는 척) 하기
Jpa 잘 (하는 척) 하기Jpa 잘 (하는 척) 하기
Jpa 잘 (하는 척) 하기경원 이
 
Content Management With Apache Jackrabbit
Content Management With Apache JackrabbitContent Management With Apache Jackrabbit
Content Management With Apache JackrabbitJukka Zitting
 
Hibernate Presentation
Hibernate  PresentationHibernate  Presentation
Hibernate Presentationguest11106b
 
Spring Boot Actuator
Spring Boot ActuatorSpring Boot Actuator
Spring Boot ActuatorRowell Belen
 
HTTP Request and Response Structure
HTTP Request and Response StructureHTTP Request and Response Structure
HTTP Request and Response StructureBhagyashreeGajera1
 
Data Persistence in Android with Room Library
Data Persistence in Android with Room LibraryData Persistence in Android with Room Library
Data Persistence in Android with Room LibraryReinvently
 

Mais procurados (20)

Servlet and jsp interview questions
Servlet and jsp interview questionsServlet and jsp interview questions
Servlet and jsp interview questions
 
Ajax and Jquery
Ajax and JqueryAjax and Jquery
Ajax and Jquery
 
Android Task Hijacking
Android Task HijackingAndroid Task Hijacking
Android Task Hijacking
 
Java Persistence API (JPA) Step By Step
Java Persistence API (JPA) Step By StepJava Persistence API (JPA) Step By Step
Java Persistence API (JPA) Step By Step
 
PHP para Adultos: Clean Code e Object Calisthenics
PHP para Adultos: Clean Code e Object CalisthenicsPHP para Adultos: Clean Code e Object Calisthenics
PHP para Adultos: Clean Code e Object Calisthenics
 
PowerShell for Penetration Testers
PowerShell for Penetration TestersPowerShell for Penetration Testers
PowerShell for Penetration Testers
 
Performance Analysis: The USE Method
Performance Analysis: The USE MethodPerformance Analysis: The USE Method
Performance Analysis: The USE Method
 
Keeping Spark on Track: Productionizing Spark for ETL
Keeping Spark on Track: Productionizing Spark for ETLKeeping Spark on Track: Productionizing Spark for ETL
Keeping Spark on Track: Productionizing Spark for ETL
 
Second Level Cache in JPA Explained
Second Level Cache in JPA ExplainedSecond Level Cache in JPA Explained
Second Level Cache in JPA Explained
 
Heap Dump Analysis - AEM: Real World Issues
Heap Dump Analysis - AEM: Real World IssuesHeap Dump Analysis - AEM: Real World Issues
Heap Dump Analysis - AEM: Real World Issues
 
Java Concurrency by Example
Java Concurrency by ExampleJava Concurrency by Example
Java Concurrency by Example
 
Outlook and Exchange for the bad guys
Outlook and Exchange for the bad guysOutlook and Exchange for the bad guys
Outlook and Exchange for the bad guys
 
Jpa 잘 (하는 척) 하기
Jpa 잘 (하는 척) 하기Jpa 잘 (하는 척) 하기
Jpa 잘 (하는 척) 하기
 
Content Management With Apache Jackrabbit
Content Management With Apache JackrabbitContent Management With Apache Jackrabbit
Content Management With Apache Jackrabbit
 
Java 9 Features
Java 9 FeaturesJava 9 Features
Java 9 Features
 
Hibernate Presentation
Hibernate  PresentationHibernate  Presentation
Hibernate Presentation
 
Python programming : Threads
Python programming : ThreadsPython programming : Threads
Python programming : Threads
 
Spring Boot Actuator
Spring Boot ActuatorSpring Boot Actuator
Spring Boot Actuator
 
HTTP Request and Response Structure
HTTP Request and Response StructureHTTP Request and Response Structure
HTTP Request and Response Structure
 
Data Persistence in Android with Room Library
Data Persistence in Android with Room LibraryData Persistence in Android with Room Library
Data Persistence in Android with Room Library
 

Destaque

Dapper - Rise of the MicroORM
Dapper - Rise of the MicroORMDapper - Rise of the MicroORM
Dapper - Rise of the MicroORMSquareHire
 
Object Relational Mapping with Dapper (Micro ORM)
Object Relational Mapping with Dapper (Micro ORM)Object Relational Mapping with Dapper (Micro ORM)
Object Relational Mapping with Dapper (Micro ORM)Muhammad Umar
 
Repository and Unit Of Work Design Patterns
Repository and Unit Of Work Design PatternsRepository and Unit Of Work Design Patterns
Repository and Unit Of Work Design PatternsHatim Hakeel
 
Introduction the Repository Pattern
Introduction the Repository PatternIntroduction the Repository Pattern
Introduction the Repository PatternBill Lin
 
Dapper & Dapper.SimpleCRUD
Dapper & Dapper.SimpleCRUDDapper & Dapper.SimpleCRUD
Dapper & Dapper.SimpleCRUDBlank Chen
 
Real World Dependency Injection - phpday
Real World Dependency Injection - phpdayReal World Dependency Injection - phpday
Real World Dependency Injection - phpdayStephan Hochdörfer
 
Difference between wcf and asp.net web api
Difference between wcf and asp.net web apiDifference between wcf and asp.net web api
Difference between wcf and asp.net web apiUmar Ali
 
Dependency injection in asp.net core
Dependency injection in asp.net coreDependency injection in asp.net core
Dependency injection in asp.net coreBill Lin
 
Generic repository pattern with ASP.NET MVC and Entity Framework
Generic repository pattern with ASP.NET MVC and Entity FrameworkGeneric repository pattern with ASP.NET MVC and Entity Framework
Generic repository pattern with ASP.NET MVC and Entity FrameworkMd. Mahedee Hasan
 
Real World Asp.Net WebApi Applications
Real World Asp.Net WebApi ApplicationsReal World Asp.Net WebApi Applications
Real World Asp.Net WebApi ApplicationsEffie Arditi
 

Destaque (11)

Dapper - Rise of the MicroORM
Dapper - Rise of the MicroORMDapper - Rise of the MicroORM
Dapper - Rise of the MicroORM
 
Object Relational Mapping with Dapper (Micro ORM)
Object Relational Mapping with Dapper (Micro ORM)Object Relational Mapping with Dapper (Micro ORM)
Object Relational Mapping with Dapper (Micro ORM)
 
Repository and Unit Of Work Design Patterns
Repository and Unit Of Work Design PatternsRepository and Unit Of Work Design Patterns
Repository and Unit Of Work Design Patterns
 
Introduction the Repository Pattern
Introduction the Repository PatternIntroduction the Repository Pattern
Introduction the Repository Pattern
 
Dapper & Dapper.SimpleCRUD
Dapper & Dapper.SimpleCRUDDapper & Dapper.SimpleCRUD
Dapper & Dapper.SimpleCRUD
 
Dapper + QueryObject
Dapper + QueryObjectDapper + QueryObject
Dapper + QueryObject
 
Real World Dependency Injection - phpday
Real World Dependency Injection - phpdayReal World Dependency Injection - phpday
Real World Dependency Injection - phpday
 
Difference between wcf and asp.net web api
Difference between wcf and asp.net web apiDifference between wcf and asp.net web api
Difference between wcf and asp.net web api
 
Dependency injection in asp.net core
Dependency injection in asp.net coreDependency injection in asp.net core
Dependency injection in asp.net core
 
Generic repository pattern with ASP.NET MVC and Entity Framework
Generic repository pattern with ASP.NET MVC and Entity FrameworkGeneric repository pattern with ASP.NET MVC and Entity Framework
Generic repository pattern with ASP.NET MVC and Entity Framework
 
Real World Asp.Net WebApi Applications
Real World Asp.Net WebApi ApplicationsReal World Asp.Net WebApi Applications
Real World Asp.Net WebApi Applications
 

Semelhante a Dapper performance

Кирилл Безпалый, .NET Developer, Ciklum
Кирилл Безпалый, .NET Developer, CiklumКирилл Безпалый, .NET Developer, Ciklum
Кирилл Безпалый, .NET Developer, CiklumAlina Vilk
 
Developing on SQL Azure
Developing on SQL AzureDeveloping on SQL Azure
Developing on SQL AzureIke Ellis
 
DjangoCon 2010 Scaling Disqus
DjangoCon 2010 Scaling DisqusDjangoCon 2010 Scaling Disqus
DjangoCon 2010 Scaling Disquszeeg
 
Scaling asp.net websites to millions of users
Scaling asp.net websites to millions of usersScaling asp.net websites to millions of users
Scaling asp.net websites to millions of usersoazabir
 
Migration to ClickHouse. Practical guide, by Alexander Zaitsev
Migration to ClickHouse. Practical guide, by Alexander ZaitsevMigration to ClickHouse. Practical guide, by Alexander Zaitsev
Migration to ClickHouse. Practical guide, by Alexander ZaitsevAltinity Ltd
 
Jdbc oracle
Jdbc oracleJdbc oracle
Jdbc oracleyazidds2
 
CBStreams - Java Streams for ColdFusion (CFML)
CBStreams - Java Streams for ColdFusion (CFML)CBStreams - Java Streams for ColdFusion (CFML)
CBStreams - Java Streams for ColdFusion (CFML)Ortus Solutions, Corp
 
ITB2019 CBStreams : Accelerate your Functional Programming with the power of ...
ITB2019 CBStreams : Accelerate your Functional Programming with the power of ...ITB2019 CBStreams : Accelerate your Functional Programming with the power of ...
ITB2019 CBStreams : Accelerate your Functional Programming with the power of ...Ortus Solutions, Corp
 
Advanced .NET Data Access with Dapper
Advanced .NET Data Access with Dapper Advanced .NET Data Access with Dapper
Advanced .NET Data Access with Dapper David Paquette
 
My Database Skills Killed the Server
My Database Skills Killed the ServerMy Database Skills Killed the Server
My Database Skills Killed the ServerColdFusionConference
 
Azure SQL Database - Connectivity Best Practices
Azure SQL Database - Connectivity Best PracticesAzure SQL Database - Connectivity Best Practices
Azure SQL Database - Connectivity Best PracticesJose Manuel Jurado Diaz
 
How to use the new Domino Query Language
How to use the new Domino Query LanguageHow to use the new Domino Query Language
How to use the new Domino Query LanguageTim Davis
 
Practical catalyst
Practical catalystPractical catalyst
Practical catalystdwm042
 
Presentation of OrientDB v2.2 - Webinar
Presentation of OrientDB v2.2 - WebinarPresentation of OrientDB v2.2 - Webinar
Presentation of OrientDB v2.2 - WebinarOrient Technologies
 

Semelhante a Dapper performance (20)

Кирилл Безпалый, .NET Developer, Ciklum
Кирилл Безпалый, .NET Developer, CiklumКирилл Безпалый, .NET Developer, Ciklum
Кирилл Безпалый, .NET Developer, Ciklum
 
Developing on SQL Azure
Developing on SQL AzureDeveloping on SQL Azure
Developing on SQL Azure
 
DjangoCon 2010 Scaling Disqus
DjangoCon 2010 Scaling DisqusDjangoCon 2010 Scaling Disqus
DjangoCon 2010 Scaling Disqus
 
Scaling asp.net websites to millions of users
Scaling asp.net websites to millions of usersScaling asp.net websites to millions of users
Scaling asp.net websites to millions of users
 
Orms vs Micro-ORMs
Orms vs Micro-ORMsOrms vs Micro-ORMs
Orms vs Micro-ORMs
 
Migration to ClickHouse. Practical guide, by Alexander Zaitsev
Migration to ClickHouse. Practical guide, by Alexander ZaitsevMigration to ClickHouse. Practical guide, by Alexander Zaitsev
Migration to ClickHouse. Practical guide, by Alexander Zaitsev
 
Performance Tuning
Performance TuningPerformance Tuning
Performance Tuning
 
Jdbc oracle
Jdbc oracleJdbc oracle
Jdbc oracle
 
CBStreams - Java Streams for ColdFusion (CFML)
CBStreams - Java Streams for ColdFusion (CFML)CBStreams - Java Streams for ColdFusion (CFML)
CBStreams - Java Streams for ColdFusion (CFML)
 
ITB2019 CBStreams : Accelerate your Functional Programming with the power of ...
ITB2019 CBStreams : Accelerate your Functional Programming with the power of ...ITB2019 CBStreams : Accelerate your Functional Programming with the power of ...
ITB2019 CBStreams : Accelerate your Functional Programming with the power of ...
 
Ado
AdoAdo
Ado
 
Advanced .NET Data Access with Dapper
Advanced .NET Data Access with Dapper Advanced .NET Data Access with Dapper
Advanced .NET Data Access with Dapper
 
My Database Skills Killed the Server
My Database Skills Killed the ServerMy Database Skills Killed the Server
My Database Skills Killed the Server
 
Azure SQL Database - Connectivity Best Practices
Azure SQL Database - Connectivity Best PracticesAzure SQL Database - Connectivity Best Practices
Azure SQL Database - Connectivity Best Practices
 
How to use the new Domino Query Language
How to use the new Domino Query LanguageHow to use the new Domino Query Language
How to use the new Domino Query Language
 
Practical catalyst
Practical catalystPractical catalyst
Practical catalyst
 
Presentation of OrientDB v2.2 - Webinar
Presentation of OrientDB v2.2 - WebinarPresentation of OrientDB v2.2 - Webinar
Presentation of OrientDB v2.2 - Webinar
 
Spring data requery
Spring data requerySpring data requery
Spring data requery
 
For Beginers - ADO.Net
For Beginers - ADO.NetFor Beginers - ADO.Net
For Beginers - ADO.Net
 
What's New in Apache Hive
What's New in Apache HiveWhat's New in Apache Hive
What's New in Apache Hive
 

Último

The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxLoriGlavin3
 
Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rick Flair
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Farhan Tariq
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxLoriGlavin3
 
Manual 508 Accessibility Compliance Audit
Manual 508 Accessibility Compliance AuditManual 508 Accessibility Compliance Audit
Manual 508 Accessibility Compliance AuditSkynet Technologies
 
Assure Ecommerce and Retail Operations Uptime with ThousandEyes
Assure Ecommerce and Retail Operations Uptime with ThousandEyesAssure Ecommerce and Retail Operations Uptime with ThousandEyes
Assure Ecommerce and Retail Operations Uptime with ThousandEyesThousandEyes
 
Modern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better StrongerModern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better Strongerpanagenda
 
Time Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsTime Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsNathaniel Shimoni
 
A Framework for Development in the AI Age
A Framework for Development in the AI AgeA Framework for Development in the AI Age
A Framework for Development in the AI AgeCprime
 
What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfMounikaPolabathina
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .Alan Dix
 
Data governance with Unity Catalog Presentation
Data governance with Unity Catalog PresentationData governance with Unity Catalog Presentation
Data governance with Unity Catalog PresentationKnoldus Inc.
 
Testing tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examplesTesting tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examplesKari Kakkonen
 
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...Scott Andery
 
2024 April Patch Tuesday
2024 April Patch Tuesday2024 April Patch Tuesday
2024 April Patch TuesdayIvanti
 
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentEmixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentPim van der Noll
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxLoriGlavin3
 
Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersRaghuram Pandurangan
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 

Último (20)

The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
 
Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
 
Manual 508 Accessibility Compliance Audit
Manual 508 Accessibility Compliance AuditManual 508 Accessibility Compliance Audit
Manual 508 Accessibility Compliance Audit
 
Assure Ecommerce and Retail Operations Uptime with ThousandEyes
Assure Ecommerce and Retail Operations Uptime with ThousandEyesAssure Ecommerce and Retail Operations Uptime with ThousandEyes
Assure Ecommerce and Retail Operations Uptime with ThousandEyes
 
Modern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better StrongerModern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
 
Time Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsTime Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directions
 
A Framework for Development in the AI Age
A Framework for Development in the AI AgeA Framework for Development in the AI Age
A Framework for Development in the AI Age
 
What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdf
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .
 
Data governance with Unity Catalog Presentation
Data governance with Unity Catalog PresentationData governance with Unity Catalog Presentation
Data governance with Unity Catalog Presentation
 
Testing tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examplesTesting tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examples
 
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
 
2024 April Patch Tuesday
2024 April Patch Tuesday2024 April Patch Tuesday
2024 April Patch Tuesday
 
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentEmixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
 
Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information Developers
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 

Dapper performance

  • 2. Agenda • Query Caching • Buffering • Query Vs QueryMultiple • Dirty Tracking • Q & A
  • 3. Query Caching • Dapper caches information about every query it runs, this allow it to materialize objects quickly and process parameters quickly. • The current implementation caches this information in a ConcurrentDictionary object. static readonly ConcurrentDictionary<Identity, CacheInfo> _queryCache = new ConcurrentDictionary<Identity, CacheInfo>(); • The objects it stores are never flushed. • If you are generating SQL strings on the fly without using parameters it is possible you will hit memory issues. • Each query you issue will create an Identity, depending on the SQL query, its command type and its parameters. • The CacheInfo object contains IDataReader and IDBCommand functions and some counters which limit the cached amount.
  • 4. Cont… • The Identity class which is used for caching will look like as mentioned in the below slide,
  • 5. private Identity(string sql, CommandType? commandType, string connectionString, Type type, Type parametersType, Type[] otherTypes, int gridIndex) { this.sql = sql; this.commandType = commandType; this.connectionString = connectionString; this.type = type; this.parametersType = parametersType; this.gridIndex = gridIndex; unchecked { hashCode = 17; // we *know* we are using this in a dictionary, so pre-compute this hashCode = hashCode * 23 + commandType.GetHashCode(); hashCode = hashCode * 23 + gridIndex.GetHashCode(); hashCode = hashCode * 23 + (sql == null ? 0 : sql.GetHashCode()); hashCode = hashCode * 23 + (type == null ? 0 : type.GetHashCode()); if (otherTypes != null) { foreach (var t in otherTypes) { hashCode = hashCode * 23 + (t == null ? 0 : t.GetHashCode()); } } hashCode = hashCode * 23 + (connectionString == null ? 0 : connectionString.GetHashCode()); hashCode = hashCode * 23 + (parametersType == null ? 0 : parametersType.GetHashCode()); } }
  • 6. CacheInfo Class class CacheInfo { public Func<IDataReader, object> Deserializer { get; set; } public Func<IDataReader, object>[] OtherDeserializers { get; set; } public Action<IDbCommand, object> ParamReader { get; set; } private int hitCount; public int GetHitCount() { return Interlocked.CompareExchange(ref hitCount, 0, 0); } public void RecordHit() { Interlocked.Increment(ref hitCount); } }
  • 7. Note on Caching • Use this string s = "SELECT email, passwd, login_id, full_name " + "FROM members WHERE " + "email = @email"; SqlCommand cmd = new SqlCommand(s); cmd.Parameters.Add("@email", email); • Instead of cmd.CommandText = "SELECT email, passwd, login_id, full_name " + "FROM members " + "WHERE email = '" + email + "'"; • The first one is parameterized. It will be cached once. The second one is not parameterized. It will be cached every time you write a query like it with a different value for email. This will explode your memory. (+) • The first one is vastly superior. It avoids injection attacks. dapper can cache it once. SQL Server will compile the execution plan once and cache it. (+)
  • 8. Buffering • The buffer is unrelated to cache. • Dapper does not include any kind of data-cache (although it does have a cache related to the way how it processes commands, i.e. "this command string, with this type of parameter, and this type of entity - has these associated dynamically generated methods to configure the command and populate the objects"). • It’s a bool value, supplied against each command object. By default buffering set to true.
  • 9. Buffer = true • In a buffered API all the rows are read before anything is yielded. • When dealing with the limited no of rows for example 100 or 200. So that it consumes less memory. • Once you get the data, the command is complete - so there is no conflict between that and subsequent operations. (+) • It doesn’t not hold the active connection for a long time. (+) • As soon as you get the data, the command has already released any resources (locks etc), so you're having minimal impact on the server. (+) • If the query is immense, loading them all into memory (in a list) could be expensive / impossible. (-) • High latency time. (-)
  • 10. Buffer = false • If at all dealing with large amount of data (thousands to millions of rows). It consumes lot of memory for storing the buffered data. • you can iterate over immense queries (many millions of rows), without needing them all in-memory at once - since you're only ever really looking at the current row being yielded. (+) • In a streaming API each element is yielded individually. This is very memory efficient, but if you do lots of subsequent processing per item, mean that your connection / command could be "active" for an extended time. (+) • You don't need to wait for the end of the data to start iterating - as soon as it has at least one row. (+)
  • 11. Cont… • The connection is in-use while you're iterating, which can lead to "there is already an open reader on the connection" (or whatever the exact wording is) errors if you try to invoke other commands on a per-row basis (this can be mitigated by MARS). (-) • It holds the active connection for long time, when deals with large amount of data. (-)
  • 12. Query Vs QueryMultiple • We need to choose the right one whether to use Query or QueryMultiple. • Its completely based upon the no of resultsets expected from the command. If expected is more than one resultset we must use QueryMultiple. If not we must use Query. • QueryMultiple has some more additional logic. It applies some amount of complication.
  • 13. QueryMultiple Example var sql = @" select * from Customers where CustomerId = @id select * from Orders where CustomerId = @id select * from Returns where CustomerId = @id"; using (var multi = connection.QueryMultiple(sql, new { id = value })) { var customer = multi.Read<Customer>().Single(); var orders = multi.Read<Order>().ToList(); var returns = multi.Read<Return>().ToList(); }
  • 14. Dirty Tracking • Dapper .net provides a nice feature to determine if the update statement is really required. If the value does not change, it won’t generate the SQL statement, which is very handy performance optimization. • The only requirement is we need to declare a interface for the object.
  • 15. Update – Without Tracking using (var sqlConnection = new SqlConnection(Constant.DatabaseConnection)) { sqlConnection.Open(); var entity = sqlConnection.Get(9); entity.ContactName = "John Smith"; sqlConnection.Update(entity); var result = sqlConnection.Get(9); }
  • 16. Update – With Tracking public interface ISupplier { int Id { get; set; } string CompanyName { get; set; } string ContactName { get; set; } string ContactTitle { get; set; } } public class Supplier : ISupplier { public int Id { get; set; } public string CompanyName { get; set; } public string ContactName { get; set; } public string ContactTitle { get; set; } }
  • 17. Cont… using (var sqlConnection = new SqlConnection(conString)) { sqlConnection.Open(); var supplier = sqlConnection.Get(9); Console.WriteLine(string.Format("IsUpdated {0}", sqlConnection.Update(supplier))); supplier.CompanyName = “NewManning"; Console.WriteLine(string.Format("IsUpdated {0}", sqlConnection.Update(supplier))); }
  • 18. Q & A