A3-Implement functions and aggregate data Flashcards
What is SARGable?
Search ARGument, and it means that the predicate can be executed using an index seek
What can prevent a predicate from becoming SARGable in general?
The most general form, from a SARGable perspective, for a predicate is: to, on one side have a column, and on the other side not an expression on a column.
So, In short it’s mostly about having no functions on the column and no implicit conversions of the column.
What can prevent an predicate becoming SARGable concretly?
- Any function on a column will prevent an index seek from happening, even if the function would not change the column’s value or the way the operator is applied.
- The other requirement for a predicate to be SARGable, for SQL Server at least, is that the column and expression are of the same data type or, if the data types differ, such that the expression will be implicitly converted to the data type of the column.
- Exeptions: Comparing a DATE column to a DATETIME value would normally implicitly convert the column to DATETIME (more precise data type), but that doesn’t cause index scans. In general though conversions should be explicit not left up to SQL SERVER;
- The majority are fine.
- EXIST and IN with a subquery are treated like joins, which may or may not use indexes depending on the join type chosen.
- LIKE is a slight special case. Predicates with LIKE are only SARGable if the wildcard is not at the start of the string.
SARGable expressions and performance?
SARG is an acronym of sorts for “search argument”. In plain english, a SARGable query is where you can use an index seek, because you’re looking for a single value or range of values in the index. The opposite of a SARGable query is when you perform some type of function or calculation on each row in the table, and match the result of this calculation to your search criteria. When you can’t use an index to look for a single value or range, you’re performing a table scan or index scan, which means you’re traversing the entire table or index, which is a costly operation if there are a lot of rows.
Of course, the entire performance benefit from SARGable expressions comes from having an index that covers the columns you’re querying in the first place. Without such an index, you will automatically end up with a table scan or index scan anyway.
Remarks:
- Any function used as a predicate will prevent an index seek from happening.
- Using an index is much more efficient than not using an index because SQL Server is not required to access the underlying table.
- Using Contains instead of like will improve performance for string indexing
What is the syntax of DATEFROMPARTS() function
DATEFROMPARTS(year, month, day)
Arguments:
year-An integer expression that specifies a year.
month-An integer expression that specifies a month, from 1 to 12.
day-An integer expression that specifies a day.
Remarks:
For invalid arguments, DATEFROMPARTS will raise an error. DATEFROMPARTS returns null if at least one required argument has a null value.
What is the syntax of DATEADD()
DATEADD (datepart , number , date )
datepart-The part of date to which DATEADD adds an integer number.
number-An expression that can resolve to an int that DATEADD adds to a datepart of date.
date-An expression that can resolve to one of the following values:date; datetime; datetimeoffset;datetime2; smalldatetime; time
What is the syntax, arguments and meaning of EOMONTH?
DEF: This function returns the last day of the month containing a specified date, with an optional offset.;
SYNTAX: EOMONTH( start_date [, month_to_add] );
Arguments:
start_date-A date expression that specifies the date for which to return the last day of the month.
month_to_add-An optional integer expression that specifies the number of months to add to start_date.
If the month_to_add argument has a value, then EOMONTH adds the specified number of months to start_date, and then returns the last day of the month for the resulting date. If this addition overflows the valid range of dates, then EOMONTH will raise an error.
What is datetime, datetime2, datetimeoffset?
datetime2-Defines a date that is combined with a time of day that is based on 24-hour clock. datetime2 can be considered as an extension of the existing datetime type that has a larger date range, a larger default fractional precision, and optional user-specified precision;
- When using variables even though they can be specified at datetime for example SQL SERVER will implicitly convert it to datetime2
datetime-Defines a date that is combined with a time of day with fractional seconds that is based on a 24-hour clock
- datetime values are rounded to increments of .000(-1,+1), .003(-1,+1), or .007(-2,+1) seconds, as shown in the following table.
datetimeoffset:
- Defines a date that is combined with a time of a day that has time zone awareness and is based on a 24-hour clock.
What is smalldatetime?
Defines a date that is combined with a time of day. The time is based on a 24-hour day, with seconds always zero (:00) and without fractional seconds.
Important it is recommended:
Use the time, date, datetime2 and datetimeoffset data types for new work. These types align with the SQL Standard. They are more portable. time, datetime2 and datetimeoffset provide more seconds precision. datetimeoffset provides time zone support for globally deployed applications.
What are aggregate functions?
An aggregate function performs a calculation on a set of values, and returns a single value. Except for COUNT(*), aggregate functions ignore null values. Aggregate functions are often used with the GROUP BY clause of the SELECT statement.
All aggregate functions are deterministic. In other words, aggregate functions return the same value each time that they are called, when called with a specific set of input values.
The OVER clause may follow all aggregate functions, except the STRING_AGG, GROUPING or GROUPING_ID functions.
Use aggregate functions as expressions only in the following situations:
- The select list of a SELECT statement (either a subquery or an outer query).
- A HAVING clause.
AVG-DEF, syntax, remarks
DEF: This function returns the average of the values in a group. It ignores null values.
Syntax: AVG ( [ALL | DISTINCT] expression ) [OVER ( [ partition_by_clause] order_by_clause ) ]
Remarks:
- If the data type of expression is an alias data type, the return type is also of the alias data type. However, if the base data type of the alias data type is promoted, for example from tinyint to int, the return value will take the promoted data type, and not the alias data type.
- AVG () computes the average of a set of values by dividing the sum of those values by the count of nonnull values. If the sum exceeds the maximum value for the data type of the return value, AVG() will return an error.
- AVG is a deterministic function when used without the OVER and ORDER BY clauses. It is nondeterministic when specified with the OVER and ORDER BY clauses.
MIN-DEF, syntax, remarks
DEF:Returns the minimum value in the expression. May be followed by the OVER clause.– Aggregation Function Syntax: MIN ( [ALL | DISTINCT] expression ) –
Analytic Function Syntax: MIN ( [ALL] expression ) OVER ( [] [] )
Remarks:
- MIN ignores any null values.
- With character data columns, MIN finds the value that is lowest in the sort sequence.
- MIN is a deterministic function when used without the OVER and ORDER BY clauses. It is nondeterministic when specified with the OVER and ORDER BY clauses.
COUNT_BIG-DEF, syntax, remarks
DEF: This function returns the number of items found in a group. COUNT_BIG operates like the COUNT function. These functions differ only in the data types of their return values. COUNT_BIG always returns a bigint data type value. COUNT always returns an int data type value.
– Aggregation Function Syntax: COUNT_BIG ( { [[ ALL | DISTINCT] expression ] | * } )
– Analytic Function Syntax:COUNT_BIG ( [ALL] { expression | * } ) OVER ( [] )
Remarks:
COUNT_BIG(*) returns the number of items in a group. This includes NULL values and duplicates.
COUNT_BIG (ALL expression) evaluates expression for each row in a group, and returns the number of nonnull values.
COUNT_BIG (DISTINCT expression) evaluates expression for each row in a group, and returns the number of unique, nonnull values.
COUNT_BIG is a deterministic function when used without the OVER and ORDER BY clauses. COUNT_BIG is nondeterministic when used with the OVER and ORDER BY clauses
TOP-def, syntax, remarks
def: Limits the rows returned in a query result set to a specified number of rows or percentage of rows in SQL Server 2019 (15.x). When you use TOP with the ORDER BY clause, the result set is limited to the first N number of ordered rows. Otherwise, TOP returns the first N number of rows in an undefined order. Use this clause to specify the number of rows returned from a SELECT statement. Or, use TOP to specify the rows affected by an INSERT, UPDATE, MERGE, or DELETE statement.
syntax: [TOP (expression) [PERCENT] [WITH TIES] ]
Best Practices:
In a SELECT statement, always use an ORDER BY clause with the TOP clause. Because, it’s the only way to predictably indicate which rows are affected by TOP.
Use OFFSET and FETCH in the ORDER BY clause instead of the TOP clause to implement a query paging solution. A paging solution (that is, sending chunks or “pages” of data to the client) is easier to implement using OFFSET and FETCH clauses. For more information, see ORDER BY Clause (Transact-SQL).
Use TOP (or OFFSET and FETCH) instead of SET ROWCOUNT to limit the number of rows returned. These methods are preferred over using SET ROWCOUNT for the following reasons:
- As a part of a SELECT statement, the query optimizer can consider the value of expression in the TOP or FETCH clauses during query optimization. Because you use SET ROWCOUNT outside of a statement that runs a query, its value can’t be considered in a query plan.
AT TIME ZONE-def, syntax, remarks
def: converts an inputdate to the corresponding datetimeoffset value in the target time zone. When inputdate is provided without offset information, the function applies the offset of the time zone assuming that inputdate is in the target time zone. If inputdate is provided as a datetimeoffset value, then AT TIME ZONE clause converts it into the target time zone using the time zone conversion rules.
syntax: inputdate AT TIME ZONE timezone
remarks: Works with windows registry on the local time machine and is time aware of daylight saving time.