MySQL(四)

來源:互聯網
上載者:User

標籤:mysqld   dex   --   類型   nec   崩潰恢複   proc   nod   int   

資料庫儲存引擎一 什麼是儲存引擎

mysql中建立的庫===>檔案夾

庫中建立的表===>檔案

現實生活中我們用來儲存資料的檔案應該有不同的類型:比如存文本用txt類型,存表格用excel,存圖片用png等

資料庫中的表也應該有不同的類型,表的類型不同,會對應mysql不同的存取機制,表類型又稱為儲存引擎。

儲存引擎說白了就是如何儲存資料、如何為儲存的資料建立索引和如何更新、查詢資料等技術的實現方
法。因為在關聯式資料庫中資料的儲存是以表的形式儲存的,所以儲存引擎也可以稱為表類型(即儲存和
操作此表的類型)

在Oracle 和SQL Server等資料庫中只有一種儲存引擎,所有資料存放區管理機制都是一樣的。而MySql
資料庫提供了多種儲存引擎。使用者可以根據不同的需求為資料表選擇不同的儲存引擎,使用者也可以根據
自己的需要編寫自己的儲存引擎

二 mysql支援的儲存引擎
MariaDB [(none)]> show engines\G  #查看所有支援的儲存引擎MariaDB [(none)]> show variables like ‘storage_engine%‘; #查看正在使用的儲存引擎

MySQL常用的儲存引擎
MyISAM儲存引擎
由於該儲存引擎不支援事務、也不支援外鍵,所以訪問速度較快。因此當對事務完整性沒有要求並以訪問為主的應用適合使用該儲存引擎。

InnoDB儲存引擎(主要使用)
由於該儲存引擎在事務上具有優勢,即支援具有提交、復原及崩潰恢複能力等事務特性,所以比MyISAM儲存引擎佔用更多的磁碟空間。因此當需要頻繁的更新、刪除操作,同時還對事務的完整性要求較高,需要實現並發控制,建議選擇。

MEMORY
MEMORY儲存引擎儲存資料的位置是記憶體,因此訪問速度最快,但是安全上沒有保障。適合於需要快速的訪問或暫存資料表。

BLACKHOLE
黑洞儲存引擎,可以應用於主備複製中的分發主庫。

三 使用儲存引擎

方法1:建表時指定

MariaDB [db1]> create table innodb_t1(id int,name char)engine=innodb;MariaDB [db1]> create table innodb_t2(id int)engine=innodb;MariaDB [db1]> show create table innodb_t1;MariaDB [db1]> show create table innodb_t2;

方法2:在設定檔中指定預設的儲存引擎

/etc/my.cnf[mysqld]default-storage-engine=INNODBinnodb_file_per_table=1

查看

[[email protected] db1]# cd /var/lib/mysql/db1/[[email protected] db1]# lsdb.opt  innodb_t1.frm  innodb_t1.ibd  innodb_t2.frm  innodb_t2.ibd

四 預存程序

預存程序是一個SQL語句集合,當主動去調用預存程序時,其中內部的SQL語句會按照邏輯執行。

 建立預存程序
-- 建立預存程序delimiter //create procedure p1()BEGIN    select * from t1;END//delimiter ;-- 執行預存程序call p1()
索引一 索引簡介 
 

索引在MySQL中也叫做“鍵”,是儲存引擎用於快速找到記錄的一種資料結構。索引對於良好的效能非常關鍵,尤其是當表中的資料量越來越大時,索引對於效能的影響愈發重要。

索引最佳化應該是對查詢效能最佳化最有效手段了。
索引能夠輕易將查詢效能提高好幾個數量級。
索引相當於字典的音序表,如果要查某個字,如果不使用音序表,則需要從幾百頁中逐頁去查。

索引特點:建立與維護索引會消耗很多時間與磁碟空間,但查詢速度大大提高!

二 索引文法建立索引
--建立表時--文法:    CREATE TABLE 表名 (                欄位名1  資料類型 [完整性條件約束條件…],                欄位名2  資料類型 [完整性條件約束條件…],                [UNIQUE]   INDEX | KEY                [索引名]  (欄位名[(長度)]  [ASC |DESC])                );----------------------------------建立普通索引樣本:    CREATE TABLE emp1 (        id INT,        name VARCHAR(30) ,        resume VARCHAR(50),        INDEX index_emp_name (name)    --KEY index_dept_name (dept_name)        );--建立唯一索引樣本:    CREATE TABLE emp2 (        id INT,        name VARCHAR(30) ,        bank_num CHAR(18) UNIQUE ,        resume VARCHAR(50),        UNIQUE INDEX index_emp_name (name)        );--建立全文索引樣本:    CREATE TABLE emp3 (        id INT,        name VARCHAR(30) ,        resume VARCHAR(50),        FULLTEXT INDEX index_resume (resume)        );--建立多列索引樣本:    CREATE TABLE emp4 (        id INT,        name VARCHAR(30) ,        resume VARCHAR(50),        INDEX index_name_resume (name,resume)        );---------------------------------
添加和刪除索引
---添加索引    ---CREATE在已存在的表上建立索引      CREATE  [UNIQUE]  INDEX  索引名              ON 表名 (欄位名[(長度)]  [ASC |DESC]) ;        ---ALTER TABLE在已存在的表上建立索引          ALTER TABLE 表名 ADD  [UNIQUE] INDEX                    索引名 (欄位名[(長度)]  [ASC |DESC]) ;             CREATE INDEX index_emp_name on emp1(name);     ALTER TABLE emp2 ADD UNIQUE INDEX index_bank_num(band_num);-- 刪除索引        文法:DROP INDEX 索引名 on 表名        DROP INDEX index_emp_name on emp1;    DROP INDEX bank_num on emp2;
三 索引測試實驗
--建立表create table Indexdb.t1(id int,name varchar(20));--預存程序delimiter $$create procedure autoinsert() BEGINdeclare i int default 1;while(i<500000)doinsert into Indexdb.t1 values(i,‘yuan‘);set i=i+1;end while;END$$delimiter ;--調用函數call autoinsert();-- 花費時間比較:-- 建立索引前   select * from Indexdb.t1 where id=300000;--0.32s-- 添加索引    create index index_id on Indexdb.t1(id);-- 建立索引後   select * from Indexdb.t1 where id=300000;--0.00s
pymsql

pymsql是Python中操作MySQL的模組,其使用方法和MySQLdb幾乎相同。

一、下載安裝:

?
1 pip3 install pymysql

二、使用

1、執行SQL ?
1234567891011121314151617181920212223242526 #!/usr/bin/env python# -*- coding:utf-8 -*-import pymysql  # 建立串連conn = pymysql.connect(host=‘127.0.0.1‘, port=3306, user=‘root‘, passwd=‘123‘, db=‘t1‘)# 建立遊標cursor = conn.cursor()  # 執行SQL,並返回收影響行數effect_row = cursor.execute("update hosts set host = ‘1.1.1.2‘")  # 執行SQL,並返回受影響行數#effect_row = cursor.execute("update hosts set host = ‘1.1.1.2‘ where nid > %s", (1,))  # 執行SQL,並返回受影響行數#effect_row = cursor.executemany("insert into hosts(host,color_id)values(%s,%s)", [("1.1.1.11",1),("1.1.1.11",2)])    # 提交,不然無法儲存建立或者修改的資料conn.commit()  # 關閉遊標cursor.close()# 關閉串連conn.close()
2、擷取新建立資料自增ID?
12345678910111213 #!/usr/bin/env python# -*- coding:utf-8 -*-import pymysql  conn = pymysql.connect(host=‘127.0.0.1‘, port=3306, user=‘root‘, passwd=‘123‘, db=‘t1‘)cursor = conn.cursor()cursor.executemany("insert into hosts(host,color_id)values(%s,%s)", [("1.1.1.11",1),("1.1.1.11",2)])conn.commit()cursor.close()conn.close()  # 擷取最新自增IDnew_id = cursor.lastrowid
3、擷取查詢資料+ View Code?
12345678910111213141516171819 #!/usr/bin/env python# -*- coding:utf-8 -*-import pymysql  conn = pymysql.connect(host=‘127.0.0.1‘, port=3306, user=‘root‘, passwd=‘123‘, db=‘t1‘)cursor = conn.cursor()cursor.execute("select * from hosts")  # 擷取第一行資料row_1 = cursor.fetchone()  # 擷取前n行資料# row_2 = cursor.fetchmany(3)# 擷取所有資料# row_3 = cursor.fetchall()  conn.commit()cursor.close()conn.close()

註:在fetch資料時按照順序進行,可以使用cursor.scroll(num,mode)來移動遊標位置,如:

  • cursor.scroll(1,mode=‘relative‘)  # 相對當前位置移動
  • cursor.scroll(2,mode=‘absolute‘) # 相對絕對位置移動
4、fetch資料類型

  關於預設擷取的資料是元祖類型,如果想要或者字典類型的資料,即:

?
123456789101112131415 #!/usr/bin/env python# -*- coding:utf-8 -*-import pymysql  conn = pymysql.connect(host=‘127.0.0.1‘, port=3306, user=‘root‘, passwd=‘123‘, db=‘t1‘)  # 遊標設定為字典類型cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)r = cursor.execute("call p1()")  result = cursor.fetchone()  conn.commit()cursor.close()conn.close()


MySQL(四)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.