Home Database Mysql Tutorial MySQL性能优化 出题业务SQL优化_MySQL

MySQL性能优化 出题业务SQL优化_MySQL

Jun 01, 2016 pm 01:20 PM
Online exam database Record

bitsCN.com

先简单介绍一下项目背景。这是一个在线考试练习平台,数据库使用MySQL,表结构如图所示:

Question是存储题目的表,数据量在3万左右。AnswerResult表是存储用户作答结果的表,分表之后单表记录大概在300万-400万。

需求:根据用户的作答结果出练习卷,题目的优先级为:未做过的题目>只做错的题目>做错又做对的题目>只做对的题目。

在“做错又做对的题目”中,会按错误次数和正确次数的比例进行权重计算,比如:A、做错10次,做对100次;B、做错10次,做对20次。这时B被选中出给用户练习的概率就大。

备注:AnswerResult表中不存在QuestionId的记录,则代表该题没有做过。

之前使用的方法:

  SELECT Question.题目标识,IFNULL((0-正确次数)/(正确次数+错误次数),1) AS 权重 FROM Question

  LEFT JOIN AnswerResult ON AnswerResult.题目标识 = Question.题目标识

  WHERE 用户标识={UserId}

说明:IFNULL((0-正确次数)/(正确次数+错误次数),1)这个函数式分2部分,

公式:(0-正确次数)/(正确次数+错误次数)得到题目的权重,这个区间为[0,-1],0表示只做错的题目,-1表示只做对的题目。IFNULL(value,1)则将未做过的题目权重设置为1,根据这个权重进行排序列出题目。

由于AnswerResult表是多达300、400百万的表,所以通过LEFT JOIN进行左连接时,迪卡尔乘积过大,又加上AnswerResult是频繁读写的表,很容易导致这条SQL变成慢查询。

性能问题被提上日程后,这条SQL语句就变成的优化点。

1、IFNULL()这个函数计算可以调整成冗余字段。

2、LEFT JOIN的迪卡尔乘积太大,可以调整为冗余或者使用INNER JOIN以提高查询速度。

3、根据需求,其实可以调整出题策略,不同的情况执行不同的SQL,而不需要在同一条SQL中实现。

解决方案针对以上三个点进行调整。虽然Question表有3万条数据,但是出题的场景其实是针对知识点出题,单个知识点题目最多也只有1000题左右,所以获取未做过的题目时,完全可以使用NOT IN走索引来完成。SQL语句如:

  A:SELECT 题目标识 FROM Question WHERE 知识点={KnowledgePointCode} AND 题目标识 NOT IN (

    SELECT 题目标识 FROM AnswerResult INNER JOIN Question AND Question.知识点={KnowledgePointCode}

    WHERE AnswerResult.用户标识 = {UserId}

  )

针对只做错的题目出题练习就简单了(正确次数 = 0代表只做错),SQL如:

  B:SELECT 题目标识 FROM AnswerResult INNER JOIN Question AND Question.知识点={KnowledgePointCode}

  WHERE AnswerResult.用户标识 = {UserId} AND 正确次数 = 0 ORDER BY 错误次数 DESC

若要对做错、做对或者只做对的题目进行出题,SQL就是这样的(已经对权重进行冗余=IFNULL((0-正确次数)/(正确次数+错误次数),1)):

  C:SELECT 题目标识 FROM AnswerResult INNER JOIN Question AND Question.知识点={KnowledgePointCode}

  WHERE AnswerResult.用户标识 = {UserId} AND 正确次数 > 0 ORDER BY 权重 DESC

 

不足:SQL语句A的查询速度依然是较慢的,虽然有缩小NOT IN的结果集,但这里还是有优化点。园子里的朋友们能不能给点建议?

有人说JOIN是SQL的性能杀手,我觉得主要还是怎么去使用JOIN,MySQL的索引优化相当重要,如果JOIN成为性能瓶颈,可以EXPLAIN看看是不是索引没有建好,并且尽量让迪卡尔乘积尽量小。使用冗余数据避免JOIN,当可能变化的冗余数据被分表之后,更新这些冗余数据就是一件非常头痛的事了。海量数据高并发,确实是一件挺头痛的事。

望园子里有这方面经验的朋友不吝赐教。谢谢。

bitsCN.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1665
14
PHP Tutorial
1269
29
C# Tutorial
1249
24
iOS 18 adds a new 'Recovered' album function to retrieve lost or damaged photos iOS 18 adds a new 'Recovered' album function to retrieve lost or damaged photos Jul 18, 2024 am 05:48 AM

Apple's latest releases of iOS18, iPadOS18 and macOS Sequoia systems have added an important feature to the Photos application, designed to help users easily recover photos and videos lost or damaged due to various reasons. The new feature introduces an album called "Recovered" in the Tools section of the Photos app that will automatically appear when a user has pictures or videos on their device that are not part of their photo library. The emergence of the "Recovered" album provides a solution for photos and videos lost due to database corruption, the camera application not saving to the photo library correctly, or a third-party application managing the photo library. Users only need a few simple steps

How does Hibernate implement polymorphic mapping? How does Hibernate implement polymorphic mapping? Apr 17, 2024 pm 12:09 PM

Hibernate polymorphic mapping can map inherited classes to the database and provides the following mapping types: joined-subclass: Create a separate table for the subclass, including all columns of the parent class. table-per-class: Create a separate table for subclasses, containing only subclass-specific columns. union-subclass: similar to joined-subclass, but the parent class table unions all subclass columns.

How to handle database connection errors in PHP How to handle database connection errors in PHP Jun 05, 2024 pm 02:16 PM

To handle database connection errors in PHP, you can use the following steps: Use mysqli_connect_errno() to obtain the error code. Use mysqli_connect_error() to get the error message. By capturing and logging these error messages, database connection issues can be easily identified and resolved, ensuring the smooth running of your application.

Detailed tutorial on establishing a database connection using MySQLi in PHP Detailed tutorial on establishing a database connection using MySQLi in PHP Jun 04, 2024 pm 01:42 PM

How to use MySQLi to establish a database connection in PHP: Include MySQLi extension (require_once) Create connection function (functionconnect_to_db) Call connection function ($conn=connect_to_db()) Execute query ($result=$conn->query()) Close connection ( $conn->close())

How to save JSON data to database in Golang? How to save JSON data to database in Golang? Jun 06, 2024 am 11:24 AM

JSON data can be saved into a MySQL database by using the gjson library or the json.Unmarshal function. The gjson library provides convenience methods to parse JSON fields, and the json.Unmarshal function requires a target type pointer to unmarshal JSON data. Both methods require preparing SQL statements and performing insert operations to persist the data into the database.

How to use database callback functions in Golang? How to use database callback functions in Golang? Jun 03, 2024 pm 02:20 PM

Using the database callback function in Golang can achieve: executing custom code after the specified database operation is completed. Add custom behavior through separate functions without writing additional code. Callback functions are available for insert, update, delete, and query operations. You must use the sql.Exec, sql.QueryRow, or sql.Query function to use the callback function.

PHP connections to different databases: MySQL, PostgreSQL, Oracle and more PHP connections to different databases: MySQL, PostgreSQL, Oracle and more Jun 01, 2024 pm 03:02 PM

PHP database connection guide: MySQL: Install the MySQLi extension and create a connection (servername, username, password, dbname). PostgreSQL: Install the PgSQL extension and create a connection (host, dbname, user, password). Oracle: Install the OracleOCI8 extension and create a connection (servername, username, password). Practical case: Obtain MySQL data, PostgreSQL query, OracleOCI8 update record.

How to handle database connections and operations using C++? How to handle database connections and operations using C++? Jun 01, 2024 pm 07:24 PM

Use the DataAccessObjects (DAO) library in C++ to connect and operate the database, including establishing database connections, executing SQL queries, inserting new records and updating existing records. The specific steps are: 1. Include necessary library statements; 2. Open the database file; 3. Create a Recordset object to execute SQL queries or manipulate data; 4. Traverse the results or update records according to specific needs.

See all articles