100 SQL Server Mistakes and How to Avoid Them MEAP V01 Peter A. Carter instant download
100 SQL Server Mistakes and How to Avoid Them MEAP V01 Peter A. Carter instant download
https://ebookmeta.com/product/100-sql-server-mistakes-and-how-to-
avoid-them-meap-v01-peter-a-carter/
https://ebookmeta.com/product/100-typescript-mistakes-and-how-to-
avoid-them-meap-v01-azat-mardan/
100 Java Mistakes and How to Avoid Them (MEAP v1) Tagir
Valeev
https://ebookmeta.com/product/100-java-mistakes-and-how-to-avoid-
them-meap-v1-tagir-valeev/
https://ebookmeta.com/product/100-go-mistakes-and-how-to-avoid-
them-1st-edition-teiva-harsanyi/
https://ebookmeta.com/product/the-ethical-implications-of-chat-
gpt-navigating-the-benefits-and-challenges-of-ai-chat-gpt-ai-
technology-application-book-3-artificial-intelligence-advisor/
Mathematical Aspects of Deep Learning Philipp Grohs
(Editor)
https://ebookmeta.com/product/mathematical-aspects-of-deep-
learning-philipp-grohs-editor/
https://ebookmeta.com/product/until-rex-1st-edition-shaw-hart/
https://ebookmeta.com/product/navigating-the-field-postgraduate-
experiences-in-social-research-1st-edition-mildred-oiza-ajebon-
yim-ming-connie-kwong-diego-astorga-de-ita-eds/
https://ebookmeta.com/product/makamsiz-individualization-of-
traditional-music-on-the-eve-of-kemalist-turkey-martin-greve/
https://ebookmeta.com/product/design-thinking-for-tech-solving-
problems-and-realizing-value-in-24-hours-1st-edition-george-
anderson/
Digital Hate The Global Conjuncture Of Extreme Speech
1st Edition Sahana Udupa
https://ebookmeta.com/product/digital-hate-the-global-
conjuncture-of-extreme-speech-1st-edition-sahana-udupa/
100 SQL Server Mistakes and How to Avoid Them
1. welcome
2. 1_Introducing_SQL_Server
3. 2_Development_Standards
4. 3_Data_Types
5. 4_Database_Design
6. index
welcome
Thank you for purchasing 100 SQL Server Mistakes and How to Avoid Them.
I hope you enjoy reading it as much as I have enjoyed writing it. To get the
very best experience from this book, however, I suggest that you do have
some prior knowledge of SQL Server. For example, I would suggest that you:
Know how to write basic queries, to read data from tables, and update
data in tables
Understand fundamental SQL Server concepts, such as Instances,
Databases and Tables
Understand the concept of encryption
Know how to perform a basic installation of SQL Server, using the GUI
I have been working with SQL Server for nearly two decades, and in that
time, I have seen lots of mistakes, that have led to problems. In this book, I
will share many of the mistakes that I have encountered and provide advice
on how to avoid making them yourself.
The primary audience for this book is the accidental SQL Server professional,
who has been thrown in the deep end, and found themselves either
developing T-SQL or administering SQL Server instances, or both!
Therefore, this book covers a range of mistakes that are made in multiple
areas, including development, administration, availability, and security.
If you have any feedback on the book, then please do reach out through the
livebook discussion forum. Your constructive feedback will help make it a
better book and your positive feedback will help keep me motivated! So, I
hope to speak to you on the forum soon, but in the meantime, thanks again
for buying the book on MEAP!
—Pete Carter
In this book
welcome 1 Introducing SQL Server 2 Development Standards 3 Data Types 4
Database Design
1 Introducing SQL Server
This chapter covers
What you should know about this book
The SQL Server index mistake (Mistake 0)
An overview of SQL Server
Why we should still care about SQL Server
Why getting SQL Server right matters
Over recent years, the DBA has become a catch-all term to describe anybody
who works with databases. I always feel that this is doing something of a
disservice to both the technology and the professionals who work with it.
SQL Server is a massive product and complex data-tier applications require
teams to have a range of competencies to harness its full power. Therefore,
when we refer to DBA in this book, we will specifically be referring to
Database Administrators.
Database Architects
Data warehouse Developers
Testers
Cyber Security Engineers
Business Intelligence (BI) Developers
How are these issues addressed? Welcome the “Accidental DBA”, a person
who has a limited amount of experience in SQL Server; often because they
are an application developer who has created some small databases as a
backend data store in the past. Suddenly, this person is responsible for the
development, optimization, administration, and security posture of all things
SQL Server related.
Does this scenario sound familiar? If so, then it is certainly worth you reading
on, as this book will explore the common mistakes made by database
professionals who are finding their feet, many of which fall into the
Accidental DBA camp.
In this book, we will discuss mistakes that are made by people performing
tasks related to development, administration, high availability (HA) and
disaster recovery (DR) as well as security.
C4 Model
As well as technical diagrams in this book, we will also make use of the C4
model, where appropriate. The C4 model is a set of architectural standard
diagrams. At its core, C4 contains four standard diagrams, consisting of a
system context diagram, a container diagram, a component diagram, and a
code diagram. The system context diagram is at the highest level, illustrating
an application’s interfaces with users and other applications. Each subsequent
level drills through a specific area of the application, providing ever more
granular detail. The lowest level of granularity being the code diagram at the
bottom.
Tip
To understand the breadth of SQL Server a little better, let’s imagine the use
case of a confectionary company called MagicChoc. They have a website,
hosted in the public cloud, which sells their chocolatey goodness. They also
have a small data center in their factory, which hosts their manufacturing
application, stock control application and reporting solution. This is
illustrated in the system landscape diagram in Figure 1.1.
C4 diagrams should always be used appropriately, and you should pick and
mix the most appropriate diagrams for any given scenario. In this chapter, the
System Landscape diagram and the Container diagram are the most
appropriate to illustrate the scenario, but we will be using other C4 diagrams
throughout the book.
You can see how the reporting application actually uses multiple SQL Server
components, in order to service the business requirements, as well as
databases which sit alongside the data warehouse and enable the functionality
of those components. A full list of the major components of SQL Server 2022
are detailed in Table 1.1.
Component Description
Analysis Services Allows for the creation and hosting of
(SSAS) multidimensional data models and tabular data
models that can be used for advanced reporting
Azure Connected Supports tight integration between Azure and SQL
Services Server hosted on-premises. This includes the ability
to easily integrate on-premises hosted SQL Server
instances with Azure features such as Synapse,
Purview, and Microsoft Defender. Additionally, the
simple Azure Arc connection can be configured
during instance installation. This allows a single
view of SQL Server installations across multi-cloud
and on-premises and fully automated technical
assessments.
Database Engine The core database management service, which
allows for relational databases to be built and
hosted. It includes second-tier operating system
components for managing memory and processor
resources. It also incorporates data security, high
availability technologies, data replication,
integrations with heterogeneous data sources, and
support for semi-structured data, such as XML and
JSON.
Data Quality A data quality solution which provides a knowledge
Services (DQS) base to support critical data quality tasks, such as
standardization and de-duplication. The DQS Server
component comprises data quality functionality and
storage, while the Data Quality Client component
provides a graphical user interface that can be used
by data domain experts.
Data Virtualization Allows developers to use T-SQL to query external
with PolyBase data sources, such as Azure Blob, Delta Tables (the
default table format in Azure Databricks), Hadoop,
MongoDB, Oracle, S3, and Teradata
Integration Services Provides versatile Extract, Transform and Load
(SSIS) (ETL) operations. Often used to pull data from
heterogeneous sources, de-normalize the data, and
populate data warehouses. Also used to integrate
data from external sources, such as web services and
FTP sites, into SQL Server databases.
Machine Learning Allows developers to use R and Python scripts
Services (In- inside databases. These scripts can be used to
Database) prepare data for machine learning, or to train,
evaluate, and deploy machine learning models.
Master Data Services A Master Data Management (MDM) solution which
(MDS) allows data stewards to manage a company’s master
data. Stewards can create data models and rules.
Master data can also be exported so that it can be
easily shared with business stakeholders.
Reporting Services Provides users with a graphical reporting tool.
(SSRS) Reports can contain tabular data, as well as charts,
maps, and other graphical elements. The reports can
be complex and there is support for parameters,
variables, linked reports, and report caching.
As you can see, SQL Server is a vast and complex product, which can and
does span many volumes. Therefore, in this book, we will focus mainly on
the database engine, although we will also touch on cloud integration and
SSIS. Therefore, let’s dive a little deeper into the database engine.
Here, the query is parsed. If you have made a mistake in the syntax of your
query, the parser is the component that will cause the error to be thrown. This
process not only checks the validity of query syntax, but also includes the
algebraization; a process which converts object names into object IDs. This
results in the creation of a highly normalized query tree, which is passed to
the Query Optimizer.
The Query Optimizer is a highly sophisticated process, which sits right at the
heart of SQL Server. The difference between SQL and many other languages,
such as C or BASIC, is that SQL is a descriptive language, rather than a
prescriptive language. In prescriptive languages, a developer specifies exactly
what he or she wants the language to do. In a descriptive language like SQL,
however, the developer simply describes the results that he or she wants to
return. It is the Query Optimizer that is responsible for determining the most
efficient way of returning the desired result set. As you can imagine, this will
have a huge impact on the performance of the Database Engine. Therefore,
the optimizer uses object types, data types, and statistics, among other
metadata, to describe the data in columns and indexes, to assess the cost of
various plans. Although the optimizer is an incredible component, it is no
substitute for well written code. You can help the optimizer to help you, by
ensuring you do not fall into T-SQL development pitfalls, such as using
cursors, which we will discuss in chapters 4 and 8. Another mistake that can
hinder the optimizer is failing to keep your statistics and indexes maintained.
We will be exploring this in chapters 9 and 10, respectively.
Tip
SQL Server 2022 can also use optimizer feedback. This uses the Query Store
and Intelligent Query Processing to optimize aspects of the query plan, such
as the memory grants and the maximum degree of parallelism, based on the
performance of a given query over time. This will be discussed further in
Chapter 9
Once a suitable plan has been established, the plan is sent to the Query
Executor. This component will interact with the Storage Engine to read (or
write) the required data. This involves the Transaction Manager, which is
responsible for managing and distributing the results of an atomic transaction.
Even though our query has not been run in the context of an explicit
transaction, it will still be inside an implicit transaction. The performance of
your transactions can be impacted if you have chosen a sub-optimal
transaction isolation level, which we will discuss in chapter 9. Lock Manager
is responsible for locking objects to ensure transactional consistency. In the
case of our query, it is possible to read rows which are never committed,
because we have used the NOLOCK query hint. We will discuss this further in
chapter 4. The Buffer Manager is the component which interacts with the
cached data in memory.
The three areas of cache which are depicted on the diagram are the Plan
Cache, which stores complex query plans, meaning that subsequent
invocations of the query may be able to avoid the optimization process. The
Log Cache caches transaction log records before they are flushed to disk.
Tip
The log records are always flushed to disk before a transaction is committed
unless Delayed Durability is used.
The Buffer Cache stores data pages that have been read from disk. It is
important to note that a query is always satisfied from the cache and never
directly from data stored on disk. Even if the required data pages do not
reside in cache, they will be read from disk into the cache and then the query
will be satisfied from the cached data. A common mistake is to return more
data in a query than you need. If you do this, then the buffer cache will fill up
quicker than it needs to. This will result in older data being released from the
cache sooner. In turn, this can lead to poor performance, as data needs to be
read from disk more often. We will discuss this more in chapter 4.
FOOTNOTES:
[138] W. H. Myers, Modern Essays (1883).
[139] George Sand, Correspondance, V. 164.
[140] Souvenirs, III. 282-3.
[141] Ibid., II. 83.
[142] That Liszt was the cause of their quarrel was well known.
Mme. Sand had written of it in her novel Horace, Mme. d’Agoult in
Nélida.
[143] For the bitterness of Mme. d’Agoult’s resentment, see
Souvenirs, II. 201-4.
[144] pp. 145-7.
[145] Correspondance, V. 220.
[146] The great George, apparently like Juliette herself, was a
believer in palmistry. See Souvenirs, II. 97.
[147] Souvenirs, III. 161, 239.
[148] Ibid.
[149] See George Sand’s letter to Flaubert, Correspondance, V.
220.
[150] It was at this restaurant that Sainte-Beuve gave that Good
Friday dinner which clerical circles regarded as a shocking
blasphemy.
[151] Souvenirs, III. 165-8.
[152] See ante, 55.
[153] Dumas was thinking of George Sand’s famous marionette
theatre at Nohant. See post, 129.
[154] Souvenirs, III. 172.
[155] Souvenirs, III. 170.
[156] George Sand, Correspondance, V. 243.
[157] Ibid., 245.
[158] Souvenirs, III. 205.
[159] Ibid., 259, and George Sand’s Correspondance, V. 258-9.
[160] Souvenirs, III. 268.
[161] George Sand, Correspondance, V. 317.
[162] Ibid., 250.
CHAPTER X
THE WAR AND PREPARATION FOR THE SIEGE OF PARIS
1870
“Nous serons vaincus. Il n’y a qu’à voir le désordre, l’impossibilité
des armements.”—George Sand to Mme. Adam, August 18, 1870.
For years a few clear-sighted Frenchmen had seen the German
Peril approaching. Now it was at the gates of France. George Sand
and Edmond Adam had been more afraid of the “Anglo-Saxon
Contagion.” They had been inclined to scoff at Nefftzer’s jeremiads;
[163] but now, alas! they proved to be only too well founded.
FOOTNOTES:
[163] See ante, 99.
[164] Ibid., 118.
[165] Souvenirs, III. 349 et seq.
[166] Souvenirs, III. 448.
[167] Souvenirs, III. 464.
[168] Ibid., 409.
[169] Ibid., 470.
[170] For Mme. Adam’s description of the grim incident which
occurred at his funeral, see Souvenirs, V. 66.
[171] Grant Duff in Notes from a Diary, September 1864.
[172] Souvenirs, III. 471.
[173] Ibid., 468.
[174] Ibid., 471.
[175] Ibid., 470.
[176] Ibid., 473.
[177] Souvenirs, IV. 2.
[178] Souvenirs, IV. 7.
[179] This diary, first published in Le Rappel, was afterwards
embodied in the series of Mme. Adam’s Souvenirs, of which it
constitutes the fourth volume, entitled Mes Illusions et nos
Souffrances pendant le siège de Paris.
[180] To Edmond de Goncourt (Journal under September 4, 1870)
the 4th of September seemed a “grey day.” Jules Favre,
Gouvernement de la Défense Nationale, I. 64, writes, “la journée ...
se leva tiède et radieuse.”
[181] Souvenirs, IV. 23.
[182] For the complete list of members of the Government, see
Jules Favre, Gouvernement de la Défense Nationale, I. 89.
[183] Souvenirs, IV. 40.
[184] Souvenirs, IV. 30.
[185] George Sand, Correspondance, VI. 29.
[186] Souvenirs, IV. 244.
[187] Souvenirs, IV. 62.
[188] Correspondance de George Sand, VI. 34.
CHAPTER XI
THE SIEGE OF PARIS
September 19, 1870-January 28, 1871
“Ce caractère parisien, qu’on peut aujourd’hui résumer en un seul mot:
héroïsme?.”—Mme. Adam, Souvenirs, December 7, 1870.