”工欲善其事,必先利其器。“—孔子《论语.录灵公》
首页 > 编程 > 用于提升数据库性能的顶级 ySQL 架构检查

用于提升数据库性能的顶级 ySQL 架构检查

发布于2024-11-19
浏览:773

A database schema defines the logical structure of your database, including tables, columns, relationships, indexes, and constraints that shape how data is organized and accessed. It’s not just about how the data is stored but also how it interacts with queries, transactions, and other operations.

These checks can help you stay on top of any new or lingering problems before they snowball into bigger issues. You can dive deeper into these schema checks below and find out exactly how to fix any issues if your database doesn't pass. Just remember, before you make any schema changes, always backup your data to protect against potential risks that might occur during modifications.

1. Primary Key Check (Missing Primary Keys)

The primary key is a critical part of any table, uniquely identifying each row and enabling efficient queries. Without a primary key, tables may experience performance issues, and certain tools like replication and schema change utilities may not function properly.

There are several issues you can avoid by defining a primary key when designing schemas:

  1. If no primary or unique key is specified, MySQL creates an internal one, which is inaccessible for usage.
  2. The lack of a primary key could slow down replication performance, especially with row-based or mixed replication.
  3. Primary keys allow scalable data archiving and purging. Tools like pt-online-schema-change require a primary or unique key.
  4. Primary keys uniquely identify rows, which is crucial from an application perspective.

Example

To create a PRIMARY KEY constraint on the "ID" column when the table is already created, use the following SQL:

ALTER TABLE Persons ADD PRIMARY KEY (ID);

To define a primary key on multiple columns:

ALTER TABLE Persons ADD CONSTRAINT PK_Person PRIMARY KEY (ID, LastName);

Note: If you use the ALTER TABLE command, then the primary key column(s) must have been declared to not contain NULL values when the table was first created.

2. Table Engine Check(Deprecated Table Engine)

The MyISAM storage engine is deprecated, and tables still using it should be migrated to InnoDB. InnoDB is the default and recommended engine for most use cases due to its superior performance, data recovery capabilities, and transaction support. Migrating from MyISAM to InnoDB can dramatically improve performance in write-heavy applications, provide better fault tolerance, and allow for more advanced MySQL features such as full-text search and foreign keys.

Why InnoDB is preferred:

  • Crash recovery capabilities allow it to recover automatically from database server or host crashes without data corruption.
  • Only locks the rows affected by a query, allowing for much better performance in high-concurrency environments.
  • Caches both data and indexes in memory, which is preferred for read-heavy workloads.
  • Fully ACID-compliant, ensuring data integrity and supporting transactions.
  • The InnoDB engine receives the majority of the focus from the MySQL development community, making it the most up-to-date and well-supported engine.

How to Migrate to InnoDB

ALTER TABLE  ENGINE=InnoDB;

3. Table Collation Check (Mixed Collations)

Using different collations across tables or even within a table can lead to performance problems, particularly during string comparisons and joins. If the collations of two string columns differ, MySQL might need to convert the strings at runtime, which can prevent indexes from being used and slow down your queries.

When you make changes to mixed collations tables, a few problems can surface:

  • Collations can differ at the column level, so mismatches at the table level won’t cause issues if the relevant columns in a join have matching collations.
  • Changing a table's collation, especially with a charset switch, isn't always simple. Data conversion might be needed, and unsupported characters could turn into corrupted data.
  • If you don’t specify a collation or charset when creating a table, it inherits the database defaults. If none are set at the database level, server defaults will apply. To avoid these issues, it’s important to standardize the collation across your entire dataset, especially for columns that are frequently used in join operations.

How to Change Collation Settings

Before making any changes to your database's collation settings, test your approach in a non-production environment to avoid unintended consequences. If you're unsure about anything, it’s best to consult with a DBA.

Retrieve the default charset and collation for all databases:

SELECT SCHEMA_NAME, DEFAULT_CHARACTER_SET_NAME, 
DEFAULT_COLLATION_NAME FROM INFORMATION_SCHEMA.SCHEMATA;

Check the collation of specific tables:

SELECT TABLE_SCHEMA, TABLE_NAME, TABLE_COLLATION FROM
information_schema.TABLES WHERE TABLE_COLLATION IS NOT NULL ORDER BY
TABLE_SCHEMA, TABLE_COLLATION;

Find the server's default charset:

SELECT @@GLOBAL.character_set_server;

Find the server's default collation:

SELECT @@GLOBAL.collation_server;

Update the collation for a specific database:

ALTER DATABASE  COLLATE=;

Update the collation for a specific table:

ALTER TABLE  COLLATE=;

4. Table Character Set Check (Mixed Character Set)

Mixed character sets are similar to mixed collations in that they can lead to performance and compatibility issues. A mixed character set occurs when different columns or tables use different encoding formats for storing data.

  • Mixed character sets can hurt join performance on string columns by preventing index use or requiring value conversions.
  • Character sets can be defined at the column level, and as long as the columns involved in a join have matching character sets, performance won’t be impacted by mismatches at the table level.
  • Changing a table’s character set may involve data conversion, which can lead to corrupted data if unsupported characters are encountered.
  • If no character set or collation is specified, tables inherit the database's defaults, and databases inherit the server's default charset and collation.

How to Change Character Settings

Before adjusting your database's character settings, be sure to test the changes in a staging environment to prevent any unexpected issues. If you're uncertain about any steps, consult a DBA for guidance.

Retrieve the default charset and collation for all databases:

SELECT SCHEMA_NAME,DEFAULT_CHARACTER_SET_NAME,
DEFAULT_COLLATION_NAME FROM INFORMATION_SCHEMA.SCHEMATA;

Get the character set of a column:

SELECT TABLE_SCHEMA, TABLE_NAME, COLUMN_NAME, CHARACTER_SET_NAME 
FROM information_schema.COLUMNS WHERE CHARACTER_SET_NAME is not NULL 
ORDER BY TABLE_SCHEMA, CHARACTER_SET_NAME;

Find the server's default charset:

SELECT @@GLOBAL.character_set_server;

Find the server's default collation:

SELECT @@GLOBAL.collation_server;

To view the structure of a table:

show create table 
;

Example output:

CREATE TABLE `
` ( `word` varchar(50) NOT NULL DEFAULT '', `sid` int(10) unsigned NOT NULL DEFAULT '0', `langcode` varchar(12) CHARACTER SET ascii NOT NULL DEFAULT '', `type` varchar(64) CHARACTER SET ascii NOT NULL, `score` float DEFAULT NULL, PRIMARY KEY (`word`,`sid`,`langcode`,`type`), ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4

To change a column character set:

ALTER TABLE  MODIFY `type` varchar(64) CHARACTER SET utf8mb4 NOT NULL;

5. Column Auto Increment Check(Type of Auto Increment Columns)

For tables that are expected to grow indefinitely and use auto-increment for primary keys, it's recommended to switch to the UNSIGNED BIGINT data type. This allows the column to handle a much larger range of values, preventing the need for costly table alterations in the future once the maximum value is reached. By specifying UNSIGNED, only positive values are stored, effectively doubling the range of the data type.

How to Change Character Settings

To modify the column type to UNSIGNED BIGINT:

ALTER TABLE .
MODIFY COLUMN id bigint unsigned NOT NULL AUTO_INCREMENT;

6. Table Foreign Key Check(Existence of foreign keys)

Foreign keys offer data consistency by maintaining the relationship between parent and child tables, but they also impact database performance. Each time a write operation occurs, additional lookups are required to verify the integrity of the related data. This can cause slowdowns, especially in high-traffic environments.

If performance is a concern, you may want to consider removing foreign keys, especially in scenarios where data consistency can be handled at the application level.

How to Remove Foreign Keys

To drop a foreign key constraint from a table:

SHOW CREATE TABLE .
; ALTER TABLE .
DROP CONSTRAINT ;

7. Duplicated Index Check

Duplicate indexes in MySQL consume unnecessary disk space and create additional overhead during write operations, as every index must be updated. This can complicate query optimization, potentially leading to inefficient execution plans without offering any real benefit.

Identify and remove duplicate indexes to streamline query optimization and reduce overhead. But make sure that the index is not being used for critical queries before removing it.

8. Unused Index Check

Unused indexes in MySQL can negatively impact database performance by consuming disk space, increasing processing overhead during inserts, updates, and deletes, and slowing down overall operations. While indexes are valuable for speeding up queries, those that aren't used can create unnecessary strain on your system.
Additional benefits of removing unused or duplicate indexes include:

  • With fewer indexes, MySQL's optimizer has fewer choices to evaluate, simplifying query execution and reducing CPU/memory usage.
  • Removing unused indexes frees up valuable disk space that can be used for more critical data, also improving I/O efficiency.
  • Index maintenance tasks, such as rebuilding or reorganizing, become faster and less resource-intensive when the number of indexes is minimized. This leads to smoother operations, particularly in environments requiring 24/7 uptime.

To identify unused indexed in MySQL or MariabDB please use to following SQL statement:

SELECT CONCAT(object_schema, '.', object_name) AS 'table', index_name
FROM performance_schema.table_io_waits_summary_by_index_usage
WHERE index_name IS NOT NULL
AND count_star = 0
AND index_name  'PRIMARY'
AND object_schema NOT IN ('mysql', 'performance_schema', 'information_schema')
ORDER BY count_star, object_schema, object_name;

How to Remove Unused or Duplicated Indexes

In MySQL 8.0 and later, you can make indexes invisible to test whether they’re needed without fully dropping them:

ALTER TABLE  ALTER INDEX  INVISIBLE;

If performance remains unaffected, the index can be safely dropped:

ALTER TABLE  DROP INDEX ;

You can revert an index back to visible if needed:

ALTER TABLE  ALTER INDEX  VISIBLE;

Schema Checks Now Available with Releem

With the latest update, Releem now includes comprehensive schema health checks. These checks provide real-time insights into your database’s structural integrity, along with actionable recommendations for fixing any detected issues.

Top ySQL Schema Checks to Boost Database Performance

By automating the schema monitoring process, Releem takes the guesswork out of manual checks, saving database engineers tons of time and effort. Instead of spending hours working on schema details, you can now focus on more pressing tasks.

版本声明 本文转载于:https://dev.to/drupaladmin/top-8-mysql-schema-checks-to-boost-database-performance-3m4k?1如有侵犯,请联系[email protected]删除
最新教程 更多>
  • Bootstrap 4 Beta 中的列偏移发生了什么?
    Bootstrap 4 Beta 中的列偏移发生了什么?
    Bootstrap 4 Beta:列偏移的删除和恢复Bootstrap 4 在其 Beta 1 版本中引入了重大更改柱子偏移了。然而,随着 Beta 2 的后续发布,这些变化已经逆转。从 offset-md-* 到 ml-auto在 Bootstrap 4 Beta 1 中, offset-md-*...
    编程 发布于2024-11-19
  • 尽管代码有效,为什么 POST 请求无法捕获 PHP 中的输入?
    尽管代码有效,为什么 POST 请求无法捕获 PHP 中的输入?
    解决 PHP 中的 POST 请求故障在提供的代码片段中:action=''而不是:action="<?php echo $_SERVER['PHP_SELF'];?>";?>"检查 $_POST数组:表单提交后使用 var_dump 检查 $_POST 数...
    编程 发布于2024-11-19
  • 为什么 GCC 无法使用初始化列表初始化 std::array?
    为什么 GCC 无法使用初始化列表初始化 std::array?
    初始化列表和 std::array:GCC BugC 标准库中的 std::array 类提供了一个固定的-大小数组容器。人们普遍认为此类支持初始化列表。但是,使用 GCC 4.6.1,尝试使用以下语法初始化 std::array 实例会失败:std::array<std::string, 2...
    编程 发布于2024-11-19
  • 为什么实体框架向导在集成 MySQL 时崩溃?
    为什么实体框架向导在集成 MySQL 时崩溃?
    Entity Framework 向导在 MySQL 数据库集成中遇到错误Entity Framework (EF) 提供数据库优先的建模功能,允许与各种SQL Server 和 MySQL 等数据库。但是,在针对 MySQL 数据库更新模型时,用户在 EF 向导期间可能会遇到崩溃。其中一个场景是当...
    编程 发布于2024-11-19
  • 我的第一个数据分析项目
    我的第一个数据分析项目
    简介和目标 在我的数据分析项目中,我进行了全面的分析工作流程,以满足现代组织对数据驱动决策日益增长的需求。我的主要目标是建立数据库连接并进行彻底的分析程序以提取有意义的见解。通过实施机器学习算法和先进的可视化技术,我开发了一个框架,将原始数据转换为可操作的情报,从而实现战略决策过程。我的方法侧重于...
    编程 发布于2024-11-19
  • 除了“if”语句之外:还有哪些地方可以在不进行强制转换的情况下使用具有显式“bool”转换的类型?
    除了“if”语句之外:还有哪些地方可以在不进行强制转换的情况下使用具有显式“bool”转换的类型?
    无需强制转换即可上下文转换为 bool您的类定义了对 bool 的显式转换,使您能够在条件语句中直接使用其实例“t”。然而,这种显式转换提出了一个问题:“t”在哪里可以在不进行强制转换的情况下用作 bool?上下文转换场景C 标准指定了四种值可以根据上下文转换为的主要场景bool:语句:if、whi...
    编程 发布于2024-11-19
  • 如何解决 Go HTTP 请求中的“需要代理验证”错误?
    如何解决 Go HTTP 请求中的“需要代理验证”错误?
    Go中HTTP请求的代理认证当使用经过认证的代理IP地址进行HTTP请求时,你可能会遇到“需要代理认证” “ 错误。要解决此问题,您需要提供代理服务器的用户名和密码。设置代理身份验证在用于请求的 HTTP 传输中,设置如下所示:auth := "username:password"...
    编程 发布于2024-11-19
  • 了解网络抓取
    了解网络抓取
    网络抓取是使用机器人从网站提取数据的过程,它涉及通过以编程方式检查所需的特定信息来从网页获取内容,其中可能包括文本、图片、价格、网址和标题。 笔记 网络抓取必须负责任地进行,尊重服务条款和法律准则,因为某些网站限制数据提取。 网页抓取的应用 电子商务 - 监控竞争对手之间的价格趋势和产品可用性 市场...
    编程 发布于2024-11-19
  • 如何使用 Launch4j 为 Java 程序创建 .exe 文件?
    如何使用 Launch4j 为 Java 程序创建 .exe 文件?
    为 Java 程序创建 .exe借助以下工具可以轻松实现将 Java 程序转换为 Windows 的 .exe Launch4j,一个免费的开源工具。操作方法如下:安装 Launch4j: 访问 Launch4j 网站并下载适合您平台的最新稳定版本。像任何其他软件一样安装它。创建 Launch4j ...
    编程 发布于2024-11-19
  • 如何使用 HTMLnd JavaScript 将条形码插入 PDF 文档
    如何使用 HTMLnd JavaScript 将条形码插入 PDF 文档
    Inserting barcodes into PDF documents can significantly streamline document management, tracking, and data processing workflows. Barcodes serve as uni...
    编程 发布于2024-11-19
  • 行偏移如何优化大型文本文件中的跳行?
    行偏移如何优化大型文本文件中的跳行?
    优化大型文本文件中的跳行在查找特定行时,逐行处理大量文本文件可能效率低下。提供的代码迭代 15MB 文件的每一行以达到所需的行号,忽略了所需行可能位于文件中较早的位置这一事实。另一种方法要解决此问题,请考虑采用利用线路偏移的优化技术。这涉及读取整个文件一次以构造一个包含每行起始偏移量的列表。Impl...
    编程 发布于2024-11-19
  • 如何在 PHP 中组合两个关联数组,同时保留唯一 ID 并处理重复名称?
    如何在 PHP 中组合两个关联数组,同时保留唯一 ID 并处理重复名称?
    在 PHP 中组合关联数组在 PHP 中,将两个关联数组组合成一个数组是一项常见任务。考虑以下请求:问题描述:提供的代码定义了两个关联数组,$array1 和 $array2。目标是创建一个新数组 $array3,它合并两个数组中的所有键值对。 此外,提供的数组具有唯一的 ID,而名称可能重合。要求...
    编程 发布于2024-11-19
  • 在 Go 中使用 WebSocket 进行实时通信
    在 Go 中使用 WebSocket 进行实时通信
    构建需要实时更新的应用程序(例如聊天应用程序、实时通知或协作工具)需要比传统 HTTP 更快、更具交互性的通信方法。这就是 WebSockets 发挥作用的地方!今天,我们将探讨如何在 Go 中使用 WebSocket,以便您可以向应用程序添加实时功能。 在这篇文章中,我们将介绍: WebSocke...
    编程 发布于2024-11-19
  • 如何使用递归宏迭代宏参数?
    如何使用递归宏迭代宏参数?
    宏参数上的 Foreach 宏在编程领域,宏提供了执行重复任务的便捷方法。然而,尝试创建一个迭代另一个宏的参数的宏可能会带来挑战。让我们探讨如何克服这个障碍并深入研究递归宏的领域。递归宏可用于遍历传递给宏的参数列表。然而,这种方法可能容易出错,因为它可能会导致无限递归。为了解决这个问题,我们引入了一...
    编程 发布于2024-11-19
  • 大批
    大批
    方法是可以在对象上调用的 fns 数组是对象,因此它们在 JS 中也有方法。 slice(begin):将数组的一部分提取到新数组中,而不改变原始数组。 let arr = ['a','b','c','d','e']; // Usecase: Extract till index p...
    编程 发布于2024-11-19

免责声明: 提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发到邮箱:[email protected] 我们会第一时间内为您处理。

Copyright© 2022 湘ICP备2022001581号-3