Nosql入门知识
1. NoSQL其实是关系型数据库相对应的,是no relational 即非关系型数据库;web2.0特别是一些用户访问量比较大的网站如:www.taobao.com weibo.com baidu.com 每秒的访问量可能是上万次(10K);传统的关系型数据库 mysql oracle 每秒进行10K次数据查询还可以勉
1. NoSQL其实是关系型数据库相对应的,是no relational 即非关系型数据库;web2.0特别是一些用户访问量比较大的网站如:www.taobao.com weibo.com baidu.com
每秒的访问量可能是上万次(10K);传统的关系型数据库 mysql oracle 每秒进行10K次数据查询还可以勉强应付,但是如果是每秒10K次读写数据库,因为数据库的数据都是卸载磁盘中,所以磁盘IO也是支撑不住每秒10K的读写。
在web的架构中,数据库是最难进行横向扩展的(通过简单的添加机器和硬件,也就是添加一些服务节点来提高负载均衡能力);对于7*24小时在线的网站来说,对关系型数据库进行升级和扩展(分布式扩展--分库分表)是非常痛苦的事情,往往要进行停机维护;但这种对www.taobao.com 来说是非常丑陋的事情。[--可不可以添加几台服务器然后把复制,然后进行负载均衡--]。
NoSQL 是采用key/value的结构来存储数据,而且大多数的NoSQL采用内存来存储数据,一段时间后把数据同步到磁盘中;由于使用内存保存数据很好地解决了高并发读写的问题;其次NoSQL提供了根据key值进行横向分表(比如:用户id,每2000w数据放到一台数据库服务器中的一张用户表中);同时实现了主从数据库互备,这样可以让数据库的动态迁移变得简单,让数据库服务器的横向扩展变得容易了。
2. 分布式数据库的CAP理论
CAP理论是说Consistency(一致性), Availability(可用性), partition tolerance(分布)三部分系统;而且任何系统只会满足两个,不会有任何的系统会同时满足这三个条件;在传统的关系型数据库中是强调C 一致性,但是在满足高可用性(高并发时效率不高),高扩展性(分布式数据库进行横向扩展)存在一定的缺陷。但是NoSQL在进行设计的时候就是针对并发海量数据存储的情况下进行设计的,在这种高并发海量数据下数据一致性并不像银行那样保持数据的强一致性,所以NoSQL·放弃强一致性的追求,从而达到更高的可用性和扩展性,通过“鸽巢原理”达到最终的一致性。
现在的数据库系统肯定是同一个时刻有多个进程对数据库进行读写操作,假设现在有3个进程(A、B、C)对数据库的某表进行操作,
强一致性:A写入的数据x,B、C可以读到数据x
弱一致性:A写入的数据x,B、C一段时间内读不到,最后会读到
最终一致性:是一种特殊的一致性,保证在一段时间内没有数据的更新,但所有的返回都是把最新的数据返回;---缓存的概念,一段时间后把数据更新到数据库,达到最终一致性。
3. 哈希算法
(1). 哈希算法的基本原理:
哈希算法的提出和应用背景,对于一个庞大的字符串数组array,给你一个字符串让你判断它是否在这个字符串数组中并找到它,最好的办法就是把这个庞大的字符串数组构建成一个哈希表,然后在进行查询是否有这个字符串。
(2).构建hash table的过程:一般是采用一个32的整数来代表一个字符串,首先这个array的字符串已经存在内存或者磁盘中,我们要做的只是按照一定的算法把每个字符串映射到一个32位的整数,每个int占4个字节,在字符串中每个字符都占一个字节;这样就建立了字符串与32位整数的映射,然后根据程序大小设定一个hash table的Size(这个Size确保所有的int % Size的值是唯一的--取最大值即可),这个把刚才得到的所有字符串对应的32位整数对这个Size进行取模,这个模值就是此整数在hash table的位置;这个位置与每一个字符串又建立了一个映射关系;这样让你查询这个str是否在array中?
首先,是把这个str,用相同的哈希算法进行编码---->映射到一个32位的int型数据 num
然后,把这个num % Size 获取此字符串在hash table里面的位置;
然后,判断hash table 此位置是否已经有数据占用,如果已经占用说明在array里面有一个字符串对应的32位整数与str的32位整数相同,在一个字符串对应唯一一个32位整数的前提条件下,就说明array里面存在字符串str。
[html]
int GetHashTablePos(char *lpszString, SOMESTRUCTURE *lpTable, int nTableSize)
{ //lpszSring--要查询的字符串;lpTable 哈希表;nTableSize是哈希表的Size
int nHash = HashString(lpszString), nHashPos = nHash % nTableSize;
if (lpTable[nHashPos].bExists && !strcmp(lpTable[nHashPos].pString, lpszString)) //时间复杂度是O(1)
return nHashPos;
else
return -1; //Error value
}
(3). 上面的处理方法是假设一个字符串通过一个哈希算法只得到唯一一个hashcode(32为int整数);但是如果存在两个整数在同一个哈希算法得到同一个hashcode,那这个查询就不正确的,虽然这个可能性比较小,但确实存在这个风险。
采用的解决办法是用多个不同的哈希算法来校验,两个str 在三个不同的哈希算法得到的hashcode都相同的概率是:1/18889465931478580854784;可以认为是OK的。
[html]
int GetHashTablePos(char *lpszString, MPQHASHTABLE *lpTable, int nTableSize)
{
const int HASH_OFFSET = 0, HASH_A = 1, HASH_B = 2;
int nHash = HashString(lpszString, HASH_OFFSET);
int nHashA = HashString(lpszString, HASH_A);
int nHashB = HashString(lpszString, HASH_B);
int nHashStart = nHash % nTableSize, nHashPos = nHashStart;
while (lpTable[nHashPos].bExists)
{
if (lpTable[nHashPos].nHashA == nHashA && lpTable[nHashPos].nHashB == nHashB)
return nHashPos;
else
nHashPos = (nHashPos + 1) % nTableSize;
if (nHashPos == nHashStart)
break;
}
return -1; //Error value
}
这样就可以保证万无一失了!
(4). 常见的哈希算法:MD5 SHA SHA-1等都是常用的哈希算法,而且他们都属于混合哈希算法,除了混合哈希算法还有加法、乘法、除法的哈希算法;
所以,在比较一个文件是否发生变化的方法出了可以用最后修改时间来判断,也可以用其哈希code来比较,比如用MD5来比较,如果其MD5都变化了则文件一定被修改了。
4. Tair 缓存也是一种 基于key/value的NoSQL结构开发的一种缓存机制,其实质也是NoSQL数据库,不过是key/value结构而且是用内存来存储数据,所以用把Tair叫做缓存。
5. 关系型数据库的事务(ACID)
(1). 事务(Transaction):Transaction是访问并可能更新数据库中各种数据项的一个程序执行单元(unit),事务一般由高级数据语言(C++ Java SQL)等写的用户程序引起的,并用begin transaction----end transaction 来界定一个完整的事务
[html]
****
****
****
transaction>
一个完整的事务由begin transaction----end transaction 里面的所有操作组成;在关系型数据库中一个事务可以是一条SQL语句或一组SQL语句或者是一个程序;事务是并发和回滚的基本单位。
(2). 事务的ACID属性:
Atomicity(原子性):一个事务是一个不可分割的完整单元,一个transaction里面的所有操作要么都做完,要么都不做;当中间一个操作失败把所有已经做的操作都回滚!www.2cto.com
Consistency(一致性):数据库在一个事务开始前是一致性的,在这个事务执行完毕后仍然是一致性的;只是从一个一致性状态到另一个一致性状态;但都是一致性的
Isolation(隔离性):一个事务的执行不能被其他事务所打扰,即一个事务内部操作及使用的数据对并发的事务是隔离的,并发执行的事务之间互相不干扰(不理解)!!
Durablity(持久性):也就永久性(Permanence),即一个事务一旦执行完毕,则它对数据库的更新是持久性的,即不受其他操作的影响;也就是事务修改了数据库了
这个ACID的属性是关系型数据库(DBMS)非常重要的属性,在执行数据库操作时必须满足ACID属性,其中AI是我们编程中要注意的地方。

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

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Kimi: In just one sentence, in just ten seconds, a PPT will be ready. PPT is so annoying! To hold a meeting, you need to have a PPT; to write a weekly report, you need to have a PPT; to make an investment, you need to show a PPT; even when you accuse someone of cheating, you have to send a PPT. College is more like studying a PPT major. You watch PPT in class and do PPT after class. Perhaps, when Dennis Austin invented PPT 37 years ago, he did not expect that one day PPT would become so widespread. Talking about our hard experience of making PPT brings tears to our eyes. "It took three months to make a PPT of more than 20 pages, and I revised it dozens of times. I felt like vomiting when I saw the PPT." "At my peak, I did five PPTs a day, and even my breathing was PPT." If you have an impromptu meeting, you should do it

In the early morning of June 20th, Beijing time, CVPR2024, the top international computer vision conference held in Seattle, officially announced the best paper and other awards. This year, a total of 10 papers won awards, including 2 best papers and 2 best student papers. In addition, there were 2 best paper nominations and 4 best student paper nominations. The top conference in the field of computer vision (CV) is CVPR, which attracts a large number of research institutions and universities every year. According to statistics, a total of 11,532 papers were submitted this year, and 2,719 were accepted, with an acceptance rate of 23.6%. According to Georgia Institute of Technology’s statistical analysis of CVPR2024 data, from the perspective of research topics, the largest number of papers is image and video synthesis and generation (Imageandvideosyn

We know that LLM is trained on large-scale computer clusters using massive data. This site has introduced many methods and technologies used to assist and improve the LLM training process. Today, what we want to share is an article that goes deep into the underlying technology and introduces how to turn a bunch of "bare metals" without even an operating system into a computer cluster for training LLM. This article comes from Imbue, an AI startup that strives to achieve general intelligence by understanding how machines think. Of course, turning a bunch of "bare metal" without an operating system into a computer cluster for training LLM is not an easy process, full of exploration and trial and error, but Imbue finally successfully trained an LLM with 70 billion parameters. and in the process accumulate

Editor of the Machine Power Report: Yang Wen The wave of artificial intelligence represented by large models and AIGC has been quietly changing the way we live and work, but most people still don’t know how to use it. Therefore, we have launched the "AI in Use" column to introduce in detail how to use AI through intuitive, interesting and concise artificial intelligence use cases and stimulate everyone's thinking. We also welcome readers to submit innovative, hands-on use cases. Video link: https://mp.weixin.qq.com/s/2hX_i7li3RqdE4u016yGhQ Recently, the life vlog of a girl living alone became popular on Xiaohongshu. An illustration-style animation, coupled with a few healing words, can be easily picked up in just a few days.

Retrieval-augmented generation (RAG) is a technique that uses retrieval to boost language models. Specifically, before a language model generates an answer, it retrieves relevant information from an extensive document database and then uses this information to guide the generation process. This technology can greatly improve the accuracy and relevance of content, effectively alleviate the problem of hallucinations, increase the speed of knowledge update, and enhance the traceability of content generation. RAG is undoubtedly one of the most exciting areas of artificial intelligence research. For more details about RAG, please refer to the column article on this site "What are the new developments in RAG, which specializes in making up for the shortcomings of large models?" This review explains it clearly." But RAG is not perfect, and users often encounter some "pain points" when using it. Recently, NVIDIA’s advanced generative AI solution

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 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())

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.
