如何写一个数据库How do you build a database?(转载)

时间:2022-06-23 01:21:48

转载自: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?(转载)的更多相关文章

  1. 用c++写一个数据库

    [cpp] view plain copy 第一步:构建一个头文件(**.h) [cpp] view plain copy #include<iostream> #include<i ...

  2. 跟我一起用Symfony写一个博客网站;

    我的微信公众号感兴趣的话可以扫一下, 或者加微信号   whenDreams 第一部分:基础设置,跑起一个页面-首页 第一步: composer create-project symfony/fram ...

  3. 如何写一个能在gulp build pipe中任意更改src内容的函数

    gulp在前端自动化构建中非常好用,有非常丰富的可以直接拿来使用的plugin,完成我们日常构建工作. 但是万事没有十全十美能够完全满足自己的需求,这时我们就要自己动手写一个小的函数,用于在gulp ...

  4. 用 Python 写一个 NoSQL 数据库Python

    NoSQL 这个词在近些年正变得随处可见. 但是到底 “NoSQL” 指的是什么? 它是如何并且为什么这么有用? 在本文, 我们将会通过纯 Python (我比较喜欢叫它, “轻结构化的伪代码”) 写 ...

  5. 用javaweb写一个注册界面,并将数据保存到后台数据库(全部完成)(课堂测试)

    一.题目:WEB界面链接数据库 1.考试要求: 1登录账号:要求由6到12位字母.数字.下划线组成,只有字母可以开头:(1分) 2登录密码:要求显示“• ”或“*”表示输入位数,密码要求八位以上字母. ...

  6. 【Part1】用JS写一个Blog(node &plus; vue &plus; mongoDB)

    学习JS也有一段时间了,准备试着写一个博客项目,前后端分离开发,后端用node只提供数据接口,前端用vue-cli脚手架搭建,路由也由前端控制,数据异步交互用vue的一个插件vue-resourse来 ...

  7. SpringBoot写一个登陆注册功能,和期间走的坑

    文章目录 前言 1. 首先介绍项目的相关技术和工具: 2. 首先创建项目 3. 项目的结构 3.1实体类: 3.2 Mapper.xml 3.3 mapper.inteface 3.4 Service ...

  8. 写一个TODO App学习Flutter本地存储工具Moor

    写一个TODO App学习Flutter本地存储工具Moor Flutter的数据库存储, 官方文档: https://flutter.dev/docs/cookbook/persistence/sq ...

  9. Spring Security 实战干货:从零手写一个验证码登录

    1. 前言 前面关于Spring Security写了两篇文章,一篇是介绍UsernamePasswordAuthenticationFilter,另一篇是介绍 AuthenticationManag ...

随机推荐

  1. C&plus;&plus; activemq CMS 学习笔记&period;

    很早前就仓促的接触过activemq,但当时太赶时间.后面发现activemq 需要了解的东西实在是太多了. 关于activemq 一直想起一遍文章.但也一直缺少自己的见解.或许是网上这些文章太多了. ...

  2. C&num; 设计模式之工厂模式(一)

    写在前面,PS一句:笔记专用,轻拍勿喷,看的不爽绕路前行即可. 一.举栗说明 1.剧情:某银行财务有三位员工,分别为A.B.C三人,主要任务就是去银行取钱,如下: class EmloyeeA: { ...

  3. linux组、用户操作相关

    Linux删除用户组和用户时常用的一些命令和参数.1.从组中删除用户编辑/etc/group 找到GROUP1那一行,删除 A或者用命令gpasswd -d A GROUP2.建用户:adduser ...

  4. &lbrack;转&rsqb; Oracle数据库备份与恢复 - 增量备份

    转:http://blog.csdn.net/pan_tian/article/details/46780929   RMAN一个强大的功能是支持增量备份,增量备份中心思想就是减少备份的数据量,我们不 ...

  5. JSP自定义标签开发入门

    一般情况下开发jsp自定义标签需要引用以下两个包 import javax.servlet.jsp.*; import javax.servlet.jsp.tagext.*; 首先我们需要大致了解开发 ...

  6. 队列的链式存储方式的实现&lpar;Java语言描述&rpar;

    链队列的结构示意图: 先进先出. QueueInterface.java//操作方法接口 package 队列的实现; public interface QueueInterface { public ...

  7. Catenyms

    poj2337:http://poj.org/problem?id=2337 题意:给定一些单词,如果一个单词的尾字母与另一个的首字母相同则可以连接.问是否可以每个单词用一次,将所有单词连接,可以则输 ...

  8. ThinkPHP 3&period;2 开发过程

    原文:ThinkPHP 3.2 开发过程 设置所有项目的公共配置Application\Common\Conf\config.php,SAE模式下配置文件为config_sae.php 配置默认的模块 ...

  9. schema for clojure

    Schema for Clojure Data Shape Declaration and Validation 1.何为schema schema是描写叙述数据形式的一种clojure数据结构,可用 ...

  10. 【HDU 4343】Interval query(倍增)

    BUPT2017 wintertraining(15) #8D 题意 给你x轴上的N个线段,M次查询,每次问你[l,r]区间里最多有多少个不相交的线段.(0<N, M<=100000) 限 ...