Getting ready for SQL Database
1.Database Growth
Size growth in 3,6,12,24,60 months
Database integrity check
Indexes -Rebuild,ReOrganize,Update Statistics
2.User Assignment and Permissions
db_datareader,db_datawriter,db_owner
App/web users, Admin users
developer persional ID to access DB
3. Physical Data File allocation
Optimization for speed?
File group and disk drive allocation
Table partitioning
4.Overcokked query
Database table partitioning in SQL Server
Create Partitioned Tables and Indexes
SQL Server and implicit conversion of types
This is the list you are after DataType Precedence
In your examples:
WHERE quantity > '3'
'3' is cast to int, matching quantity
WHERE quantityTest > 3
No casting required
WHERE date = 20120101
20120101 as a number is being cast to a date, which is too large. e.g.
select cast(20120101 as datetime)
This is different from
WHERE date = '20120101'
Where the date as a string can be cast.
If you go down a third of the CAST and CONVERT reference to the section Implicit Conversions, there is a table of implicit conversions that are allowed. Just because it is allowed doesn't mean it will work, such as (20120101 -> datetime).
SQL Wildcard Search - Efficiency?
Having the wildcard at the end of the string, like 'abc%'
, would help if that column were indexed, as it would be able to seek directly to the records which start with 'abc'
and ignore everything else. Having the wild card at the beginning means it has to look at every row, regardless of indexing.
Good article here with more explanation.
SQL Performance UNION vs OR
Either the article you read used a bad example, or you misinterpreted their point.
select username from users where company = 'bbc' or company = 'itv';
This is equivalent to:
select username from users where company IN ('bbc', 'itv');
MySQL can use an index on company
for this query just fine. There's no need to do any UNION.
The more tricky case is where you have an OR
condition that involves two different columns.
select username from users where company = 'bbc' or city = 'London';
Suppose there's an index on company
and a separate index on city
. Given that MySQL usually uses only one index per table in a given query, which index should it use? If it uses the index on company
, it would still have to do a table-scan to find rows where city
is London. If it uses the index on city
, it would have to do a table-scan for rows where company
is bbc.
The UNION
solution is for this type of case.
select username from users where company = 'bbc'
union
select username from users where city = 'London';
Now each sub-query can use the index for its search, and the results of the subquery are combined by the UNION
.
An anonymous user proposed an edit to my answer above, but a moderator rejected the edit. It should have been a comment, not an edit. The claim of the proposed edit was that UNION has to sort the result set to eliminate duplicate rows. This makes the query run slower, and the index optimization is therefore a wash.
My response is that that the indexes help to reduce the result set to a small number of rows before the UNION happens. UNION does in fact eliminate duplicates, but to do that it only has to sort the small result set. There might be cases where the WHERE clauses match a significant portion of the table, and sorting during UNION is as expensive as simply doing the table-scan. But it's more common for the result set to be reduced by the indexed searches, so the sorting is much less costly than the table-scan.
The difference depends on the data in the table, and the terms being searched. The only way to determine the best solution for a given query is to try both methods in the MySQL query profiler and compare their performance.
All purpose query
select *
from table
where column is null or column='value'
作者:Chuck Lu GitHub |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
2019-07-16 HearthBuddy Plugin编写遇到的问题
2019-07-16 HearthBuddy的plugin加载
2019-07-16 Unexpected ConvertTo-Json results? Answer: it has a default -Depth of 2
2015-07-16 WCF Host中的BaseAddress 和 Endpoint中的Address的区别
2015-07-16 使用vs自带的wcf配置工具
2015-07-16 Automatic Code Generation-->Implement Interface
2015-07-16 Learning WCF Chapter1 Exposing Multiple Service Endpoints