博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
How do you build a database?
阅读量:4576 次
发布时间:2019-06-08

本文共 4571 字,大约阅读时间需要 15 分钟。

在reddit上看到的一篇讲解数据库实现的文章,非常有意思,在这里记录一下。

 

回答者:

Its a great question, and deserves a long answer.

Most database servers are built in C, and store data using B-tree type constructs. In the old days there was a product called C-Isam (c library for an indexed sequential access method) which is a low level library to help C programmers write data in B-tree format. So you need to know about btrees and understand what these are.

Most databases store data separate to indexes. Lets assume a record (or row) is 800 bytes long and you write 5 rows of data to a file. If the row contains columns such as first name, last name, address etc. and you want to search for a specific record by last name, you can open the file and sequentially search through each record but this is very slow. Instead you open an index file which just contains the lastname and the position of the record in the data file. Then when you have the position you open the data file, lseek to that position and read the data. Because index data is very small it is much quicker to search through index files. Also as the index files are stored in btrees in it very quick to effectively do a quicksearch (divide and conquer) to find the record you are looking for.

So you understand for one "table" you will have a data file with the data and one (or many) index files. The first index file could be for lastname, the next could be to search by SS number etc. When the user defines their query to get some data, they decide which index file to search through. If you can find any info on C-ISAM (there used to be an open source version (or cheap commercial) called D-ISAM) you will understand this concept quite well.

Once you have stored data and have index files, using an ISAM type approach allows you to GET a record based on a value, or PUT a new record. However modern database servers all support SQL, so you need an SQL parser that translates the SQL statement into a sequence of related GETs. SQL may join 2 tables so an optimizer is also needed to decide which table to read first (normally based on number of rows in each table and indexes available) and how to relate it to the next table. SQL can INSERT data so you need to parse that into PUT statements but it can also combine multiple INSERTS into transactions so you need a transaction manager to control this, and you will need transaction logs to store wip/completed transactions.

It is possible you will need some backup/restore commands to backup your data files and index files and maybe also your transaction log files, and if you really want to go for it you could write some replication tools to read your transaction log and replicate the transactions to a backup database on a different server. Note if you want your client programs (for example an SQL UI like phpmyadmin) to reside on separate machine than your database server you will need to write a connection manager that sends the SQL requests over TCP/IP to your server, then authenticate it using some credentials, parse the request, run your GETS and send back the data to the client.

So these database servers can be a lot of work, especially for one person. But you can create simple versions of these tools one at a time. Start with how to store data and indexes, and how to retrieve data using an ISAM type interface.

There are books out there - look for older books on mysql and msql, look for anything on google re btrees and isam, look for open source C libraries that already do isam. Get a good understanding on file IO on a linux machine using C. Many commercial databases now dont even use the filesystem for their data files because of cacheing issues - they write directly to raw disk. You want to just write to files initially.

I hope this helps a little bit.

 

回答者:

Good lord... what an open ended question.

There's tons of literature around what makes a "good" database (normal forms). A lot of it might not make sense immediately. 

There's tons of books/guides about how to create a database model (tables, columns, relationships, etc).

There's tons of books/guides for how to implement a database model using a specific vendor's database engine (eg. how to create a table in SQL Server or Oracle or vendor_du_jour).

Maybe start with a "dummies" book. They generally give a reasonable starting point... you'll be better equipped to google/ask questions after having read one of these. 

 

注:这里只摘录了前两个得票较高的回答,大家有兴趣的话,可以直接查看原文。

参考:

https://www.reddit.com/r/Database/comments/27u6dy/how_do_you_build_a_database/

 

转载于:https://www.cnblogs.com/xiaotengyi/p/5357049.html

你可能感兴趣的文章
hdu1150 Machine Schedule (匈牙利算法模版)
查看>>
惠普 hpssacli 工具使用
查看>>
记录常用的git命令
查看>>
关于linux的/var/www/html
查看>>
Maven 环境搭建
查看>>
UIPickerView的使用
查看>>
报表属性定义
查看>>
Solr综合案例深入练习
查看>>
关于strcpy的实现.
查看>>
ReentrantLock Condition 实现消费者生产者问题
查看>>
Leetcode 89.格雷编码
查看>>
Ubuntu 16.04 安装Docker
查看>>
软件测试的几个误区
查看>>
贝塞尔曲线生成及测试
查看>>
Web开发者不可不知的15条编码原则
查看>>
mysql新建表示,时间字段timetamp碰到的问题
查看>>
【POJ2136】Vertical Histogram(简单模拟)
查看>>
二十五种网页加速方法和seo优化技巧
查看>>
随笔语录
查看>>
剑指Offer——删除链表中重复的结点
查看>>