Saturday, November 23, 2024

DB Properties - 1

 AUTO_SHRINK is a database option in SQL Server. When you enable this option for a database, this database becomes eligible for shrinking by a background task. This background task evaluates all databases that satisfy the criteria for shrinking and shrink the data or log files. 

You have to carefully evaluate setting this option for the databases in a SQL Server instance. Frequent grow and shrink operations can lead to various performance problems. 

  • If multiple databases undergo frequent shrink and grow operations, then this will easily lead to file system level fragmentation. This can have a severe impact on performance. This is true whether you use the automatic settings or whether you manually grow and shrink the files frequently. 

  • After AUTO_SHRINK successfully shrinks the data or log file, a subsequent DML or DDL operation can slow down significantly if space is required and the files need to grow. 

  • The AUTO_SHRINK background task can take up resources when there are many databases that need shrinking. 

  • The AUTO_SHRINK background task will need to acquire locks and other synchronization that can conflict with other regular application activity. 

Consider setting databases to a required size and pre-grow them. Leave the unused space in the database files if you think the application usage patterns will need them again. This can prevent frequent shrink and growth of the database files. 

 for any database, auto shrink should not be activated based on the following reasons: 

  • Executing the SQL Server auto shrink algorithms without any purpose will definitely going to squander the resources in a massive quantity. 

  • Whether you perform SQL Server auto shrink or manual shrink, it will obviously be going to cause the index fragmentation, and this eventually executes the data files shrinking also. 

  • If the server is also forcing the boundaries of the IO subsystem, running shrink may push it over, resulting in long disk queue lengths and possibly IO timeouts, this will eat up Server IO and CPU resources in a massive quantity. 

  • System performance will be retarded by file system disk-level fragmentation, which is again the direct effect of doing shrinking and growing data files often. 

 

The optimize for ad hoc workloads option is used to improve the efficiency of the plan cache for workloads that contain many single use ad hoc batches. When this option is set to 1, the Database Engine stores a small, compiled plan stub in the plan cache when a batch is compiled for the first time, instead of the full compiled plan. This option might help to relieve memory pressure by not allowing the plan cache to become filled with compiled plans that aren't reused. However, enabling this option might affect your ability to troubleshoot single-use plans. 

The compiled plan stub allows the Database Engine to recognize that this ad hoc batch was compiled previously, and only stores a compiled plan stub. When this batch is invoked (compiled or executed) again, the Database Engine compiles the batch, removes the compiled plan stub from the plan cache, and adds the full compiled plan to the plan cache. 

You can find compiled plan stubs by querying the sys.dm_exec_cached_plans catalog view and looking for "Compiled Plan" in the cacheobjtype column. The stub has a unique plan_handle. The compiled plan stub doesn't have an execution plan associated with it, and querying for the plan handle doesn't return a graphical or XML showplan. 

Recommendations 

Avoid having a large number of single-use plans in the plan cache. Common causes include: 

  • Data types of query parameters that aren't consistently defined. This particularly applies to the length of strings but can apply to any data type that has a maxlength, a precision, or a scale. For example, if a parameter named @Greeting is passed as nvarchar(10) on one call and nvarchar(20) on the next call, separate plans are created for each parameter size. 

  • Queries that aren't parameterized. If a query has one or more parameters for which hard-coded values get submitted to the Database Engine, a large number of query plans could exist for each query. Plans could exist for each combination of query parameter data types and lengths that were used. 

If the number of single-use plans take a significant portion of SQL Server Database Engine memory in an OLTP server, and these plans are ad hoc plans, use this server option to decrease memory usage with these objects. 

If the optimize for ad hoc workloads option is enabled, you can't view execution plans for single-use queries, because only the plan stub is cached. 

SELECT objtype AS [PlanCacheType], 

    COUNT_BIG(1) AS [NumOfPlans], 

    SUM(CAST(size_in_bytes AS DECIMAL(18, 2))) / 1024 / 1024 AS [PlanSizeInMB] 

FROM sys.dm_exec_cached_plans 

GROUP BY objtype 

ORDER BY NumOfPlans desc 

GO 

CPU cores and threads

 A CPU socket, also known as a CPU slot, is a component in computer hardware that provides both mechanical and electrical connections between a microprocessor and a Printed Circuit Board (PCB). It allows you to place and replace the central processing unit without requiring soldering. This feature makes it convenient for upgrading or replacing CPUs in a computer system. 

How CPU Sockets Work 

The CPU socket works by providing a socket or slot on the motherboard where the processor can be installed or placed on. The slot typically contains pins or contacts that provide electrical connections between the CPU and the motherboard. This allows the CPU to communicate with other components in the computer, such as memory, storage devices, and input/output devices. 

If you want to build a computer system, upgrade an existing system, or replace a faulty CPU, you would need a CPU socket. Without a CPU socket, it would be impossible to connect the CPU to the motherboard and other components in the computer. 

CPU Cores 

CPU cores are the processing units that are the heart of a CPU. A multi-core processor has 2 or more cores, and each of these cores is made up of billions of microscopic transistors that work together to process data. Essentially, CPU cores are the brains of a computer system, and they are responsible for executing tasks and running programs. 

Each core can handle multiple threads of instructions simultaneously, which allows the CPU to perform multiple tasks at once. In simpler terms, CPU cores can be thought of as separate processors that work together to handle different tasks simultaneously, thus increasing the overall performance of a CPU. 

 

How CPU Cores Work 

To understand how CPU cores work, it is important to first understand the basic functioning of a CPU. A CPU receives instructions from a computer’s memory, decodes those instructions, and then executes them. The execution process involves performing mathematical calculations, accessing data stored in memory, and communicating with other computer components. 

Each CPU core is designed to handle a specific set of instructions, and the CPU as a whole is responsible for coordinating the execution of all the cores. When a computer system receives a task, the CPU splits the task into smaller instructions and assigns each instruction to a different core. Each core then executes its assigned instruction, and the results are combined to complete the task. 

 

Threads 

In computing, a thread can be thought of as a single flow of execution within a program or process. A CPU thread, in particular, is a virtual representation of a physical CPU core, which is why it is often referred to as a “virtual processor.” A single CPU core can execute multiple threads, allowing for multiple flows of execution to occur simultaneously. 

This is known as multithreading, and it can significantly improve the performance of a CPU by enabling it to execute multiple tasks concurrently. 

Without threads, CPUs would only be able to execute one task at a time, and performance would suffer significantly. By allowing for multithreading, CPUs can execute multiple tasks concurrently, significantly improving performance and reducing the time it takes to complete tasks. 

 

What is Hyper-Threading 

Hyper-Threading is a technology developed by Intel that allows a single CPU core to execute multiple threads simultaneously by creating virtual cores or logical processors. This technology allows the CPU to process multiple tasks at once, which can improve overall system performance. 

Hyper-Threading is an alternative for having multiple CPUs or additional CPU cores – it acts as separate processing units whilst utilizing the same CPU hardware. 

How Does Hyper-Threading Work 

Hyper-Threading works by creating two logical processors within a single CPU core. Each logical processor appears to the operating system as a separate CPU, allowing the core to handle two threads at the same time. The physical core shares resources between the two logical processors, which allows the CPU to execute more threads than would be possible with a single physical core. 

 

Logical Processor vs. Threads 

A thread is a basic unit of CPU utilization that represents a sequence of instructions that can be executed independently by a processor. A single processor can handle multiple threads simultaneously using a technique called multithreading. 

Contrarily, a logical processor is a broader concept that encompasses both physical cores and virtual cores created through hyperthreading. Hyperthreading is a technology that allows a single physical core to handle multiple threads simultaneously, thus increasing the overall performance of the CPU. 

To understand how a logical processor works, imagine a CPU with two physical cores, each of which can handle two threads simultaneously through hyperthreading. In this case, the operating system would recognize the CPU as having four logical processors (2 physical cores x 2 threads per core). 

When a task is executed on the CPU, it is divided into multiple threads that can be executed simultaneously on different cores or virtual cores, depending on the number of logical processors available. This parallel processing enables the CPU to execute tasks more quickly and efficiently than if it were processing them sequentially.