如何写一个数据库How do you build a database?(转载)
转载自:http://www.reddit.com/r/Database/comments/27u6dy/how_do_you_build_a_database/ciggal8
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.
廖雪峰的解释:转载自http://www.ruanyifeng.com/blog/2014/07/database_implementation.html
如何写一个数据库How do you build a database?(转载)的更多相关文章
- 用c++写一个数据库
[cpp] view plain copy 第一步:构建一个头文件(**.h) [cpp] view plain copy #include<iostream> #include<i ...
- 跟我一起用Symfony写一个博客网站;
我的微信公众号感兴趣的话可以扫一下, 或者加微信号 whenDreams 第一部分:基础设置,跑起一个页面-首页 第一步: composer create-project symfony/fram ...
- 如何写一个能在gulp build pipe中任意更改src内容的函数
gulp在前端自动化构建中非常好用,有非常丰富的可以直接拿来使用的plugin,完成我们日常构建工作. 但是万事没有十全十美能够完全满足自己的需求,这时我们就要自己动手写一个小的函数,用于在gulp ...
- 用 Python 写一个 NoSQL 数据库Python
NoSQL 这个词在近些年正变得随处可见. 但是到底 “NoSQL” 指的是什么? 它是如何并且为什么这么有用? 在本文, 我们将会通过纯 Python (我比较喜欢叫它, “轻结构化的伪代码”) 写 ...
- 用javaweb写一个注册界面,并将数据保存到后台数据库(全部完成)(课堂测试)
一.题目:WEB界面链接数据库 1.考试要求: 1登录账号:要求由6到12位字母.数字.下划线组成,只有字母可以开头:(1分) 2登录密码:要求显示“• ”或“*”表示输入位数,密码要求八位以上字母. ...
- 【Part1】用JS写一个Blog(node + vue + mongoDB)
学习JS也有一段时间了,准备试着写一个博客项目,前后端分离开发,后端用node只提供数据接口,前端用vue-cli脚手架搭建,路由也由前端控制,数据异步交互用vue的一个插件vue-resourse来 ...
- SpringBoot写一个登陆注册功能,和期间走的坑
文章目录 前言 1. 首先介绍项目的相关技术和工具: 2. 首先创建项目 3. 项目的结构 3.1实体类: 3.2 Mapper.xml 3.3 mapper.inteface 3.4 Service ...
- 写一个TODO App学习Flutter本地存储工具Moor
写一个TODO App学习Flutter本地存储工具Moor Flutter的数据库存储, 官方文档: https://flutter.dev/docs/cookbook/persistence/sq ...
- Spring Security 实战干货:从零手写一个验证码登录
1. 前言 前面关于Spring Security写了两篇文章,一篇是介绍UsernamePasswordAuthenticationFilter,另一篇是介绍 AuthenticationManag ...
随机推荐
- php 二维码生成类
<?php /** * BarcodeQR - Code QR Barcode Image Generator (PNG) * @package BarcodeQR * @category Ba ...
- Android——ExpandableListView事件拦截
1.满足条件 如果使用ExpandableListView,需要子item响应一个事件,比如重新启动一个新的activity,需要满足下面的条件: (1).修改Adapter返回值 覆写BaseExp ...
- 使用Apache JMeter进行SQL优化性能测试
一. 前言 最近在公司里做性能测试,对于一张大概400万数据的表,进行全表扫描往往会比较费时,更不要说有若干这样的表格级联进行检索了.为了能够在不影响生产环境的前提下进行SQL的性能优化,需要首先利用 ...
- Qt通过odbc读取excel数据
传统的读取方式是通过Excel.Application,这种方式不仅操作繁琐,而且速度也不快. 通过odbc读取,可以使用select语句直接读取整个工作表,处理excel数据就跟数据库一样方便. 当 ...
- SeekBar和RatingBar
今天在看一个音乐播放器的源代码时候用到了SeekBar,就翻出来mars老师的视频复习了一下,然后综合使用了一下. 首先先看下运行效果: 下来我们看看布局文件的设计: main.xml: < ...
- 用Apache Ivy实现项目里的依赖管理
Apache Ivy是一个管理项目依赖的工具. 它与Maven Apache Maven 构建管理和项目管理工具已经吸引了 Java 开发人员的注意.Maven 引入了 JAR 文件公共存储库的概念 ...
- 第八章I/O
一.File的使用 ①.new File(String fileName);的意义 ②.获取当前文件夹下的所有文件 ③.获取当前文件夹时候过滤掉不许要的文件夹 ④.创建File文件,了解mkDir() ...
- centos minimal 安装无法自定义分区
安装了太多版本的linux,最后还是决定安装CentOS的minimal版本: 安装的时候发现,没办法自定义分区,经查确实在命令行界面下安装是没有自定义分区的. 但是不管怎么安装都是没有图形介面,最后 ...
- Java注释模板设置详解
设置注释模板的入口: Window->Preference->Java->Code Style->Code Template 然后展开Comments节点就是所有需设置注释的元 ...
- keil TEA
http://bbs.mydigit.cn/read.php?tid=545086 #include "reg52.h" void send_char(unsigned char ...