聊聊大数据环境下的数据库架构
随着数据的快速增长,分表,分库,memcache,redis,mongodb,hadoop,bigtable等,各种解决方案呼之欲出。经过测试,在MySQL中,无论如何加索引,数据超过百w时,查询起来耗时很明显。 因此mysql分表分库+memcache+redis也不失是一个完美解决方案。 由于redis不
随着数据的快速增长,分表,分库,memcache,redis,mongodb,hadoop,bigtable等,各种解决方案呼之欲出。经过测试,在MySQL中,无论如何加索引,数据超过百w时,查询起来耗时很明显。
因此mysql分表分库+memcache+redis也不失是一个完美解决方案。
由于redis不支持复杂查询,redis的读取性能还是赶不上mem这些劣势,因此才需要一些搭挡。
流程
从CRUD看底层架构。
1. 插入数据
一般情况下,对用户是没有必要分表的,用户的文章或者微博才分表,毕竟用户表和微博表是无法比拟,不一个等级的。
插入时,任何表,mysql只存储索引字段,其它数据存储在redis上。文章表可按一定规则进行静态分表,比如按用户id分100张表,每个用户发的文章都会路由到同一张表中。
数据总数需要一个单独的字段存储在redis中,每个用户都有一个key进行存储文章总数。有时总数数据会与实际总条数有所不同,因此需要定期执行mysql count进行更新redis中的总数。
下面是分表路由,把uid对100取余:
protected function getTableName($name, $id=null) { $tableName = self::SYS_DB_PREFIX . $name; if (is_numeric($id) && $id > 0) return $tableName . '_' . ($id % $this->tableNum); }
2. 查询数据
每第一次数据查询时,根据MySQL中的索引从redis查询,需要将查询结果存储在memcache中,下次查询直接从memcache获取。查询某人文章时,直接从一张表中进行limit查询。
关于动态页面查询,比如登录后的豆瓣首页,会有一些动态好友的更新信息。这些信息是存储在一张临时表中的,只保存10天的更新。
3. 删除数据
整个系统无任何实际操作,只进行字段状态修改。
4. 修改数据
修改数据时,需要更新对应的memcache。
分库
当系统应用更加复杂时,一台数据库服务器的压力是很大的,可根据系统的业务流进行分库,比如文章一个独立库,评论一个独立库等。
更复杂的搜索
比如招聘网站中的,按地点,按行业,按规模等更多条件查询时,就需要一定的搜索系统来完成,这里不做更多搜索细节讨论。

MySQLviewshavelimitations:1)Theydon'tsupportallSQLoperations,restrictingdatamanipulationthroughviewswithjoinsorsubqueries.2)Theycanimpactperformance,especiallywithcomplexqueriesorlargedatasets.3)Viewsdon'tstoredata,potentiallyleadingtooutdatedinforma

ProperusermanagementinMySQLiscrucialforenhancingsecurityandensuringefficientdatabaseoperation.1)UseCREATEUSERtoaddusers,specifyingconnectionsourcewith@'localhost'or@'%'.2)GrantspecificprivilegeswithGRANT,usingleastprivilegeprincipletominimizerisks.3)

MySQLdoesn'timposeahardlimitontriggers,butpracticalfactorsdeterminetheireffectiveuse:1)Serverconfigurationimpactstriggermanagement;2)Complextriggersincreasesystemload;3)Largertablesslowtriggerperformance;4)Highconcurrencycancausetriggercontention;5)M

Yes,it'ssafetostoreBLOBdatainMySQL,butconsiderthesefactors:1)StorageSpace:BLOBscanconsumesignificantspace,potentiallyincreasingcostsandslowingperformance.2)Performance:LargerrowsizesduetoBLOBsmayslowdownqueries.3)BackupandRecovery:Theseprocessescanbe

Adding MySQL users through the PHP web interface can use MySQLi extensions. The steps are as follows: 1. Connect to the MySQL database and use the MySQLi extension. 2. Create a user, use the CREATEUSER statement, and use the PASSWORD() function to encrypt the password. 3. Prevent SQL injection and use the mysqli_real_escape_string() function to process user input. 4. Assign permissions to new users and use the GRANT statement.

MySQL'sBLOBissuitableforstoringbinarydatawithinarelationaldatabase,whileNoSQLoptionslikeMongoDB,Redis,andCassandraofferflexible,scalablesolutionsforunstructureddata.BLOBissimplerbutcanslowdownperformancewithlargedata;NoSQLprovidesbetterscalabilityand

ToaddauserinMySQL,use:CREATEUSER'username'@'host'IDENTIFIEDBY'password';Here'showtodoitsecurely:1)Choosethehostcarefullytocontrolaccess.2)SetresourcelimitswithoptionslikeMAX_QUERIES_PER_HOUR.3)Usestrong,uniquepasswords.4)EnforceSSL/TLSconnectionswith

ToavoidcommonmistakeswithstringdatatypesinMySQL,understandstringtypenuances,choosetherighttype,andmanageencodingandcollationsettingseffectively.1)UseCHARforfixed-lengthstrings,VARCHARforvariable-length,andTEXT/BLOBforlargerdata.2)Setcorrectcharacters


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SublimeText3 Chinese version
Chinese version, very easy to use
