Importing and Exporting Data with MySQL

Slides:



Advertisements
Similar presentations
MySQL DBA Spring Topics in the MySQL AB DBA Course I Describe the MySQL Architecture, general operational characteristics and resources utilized.
Advertisements

VTYS 2012 Mehmet Emin KORKUSUZ Ders  Create  Alter  Drop Data Defination Language.
Drop in replacement of MySQL. Agenda MySQL branch GPL licence Maria storage engine Virtual columns FederatedX storage engine PBXT storage engine XtraDB.
Faster Than Alter – Less Downtime Chris Schneider.
MySQL-Database Teppo Räisänen Oulu University of Applied Sciences School of Business and Information Management.
Quick-and-dirty.  Commands end in a semi-colon ◦ If you forget, another prompt line shows up  Either continue the command or…  End it with a semi-colon.
1 Table Alteration. 2 Altering Tables Table definition can be altered after its creation Adding columns Changing columns’ definition Dropping columns.
A Guide to MySQL 3. 2 Objectives Start MySQL and learn how to use the MySQL Reference Manual Create a database Change (activate) a database Create tables.
SQL Basics+ Brandon Checketts. Why SQL? Structured Query Language Structured Query Language Frees programmers from dealing with specifics of data persistence.
1 CSE 480: Database Systems Lecture 9: SQL-DDL Reference: Read Chapter of the textbook.
Replication with MySQL 5.1 Ligaya Turmelle Senior Technical Support Engineer - MySQL
Information Systems Today (©2006 Prentice Hall) MySQL 1CS3754 Class Note #8, Is an open-source relational database management system 2.Is fast and.
MySQL. Dept. of Computing Science, University of Aberdeen2 In this lecture you will learn The main subsystems in MySQL architecture The different storage.
Basic SQL Commands examples from Beginning MySQL by Robert Sheldon & Geoff Moes.
Copyright 2006 MySQL AB The World’s Most Popular Open Source Database MySQL Cluster: An introduction Geert Vanderkelen MySQL AB.
CpSc 462/662: Database Management Systems (DBMS) (TEXNH Approach) Constraints, Triggers and Index James Wang.
Copyright 2002, Jeremy Zawodny MySQL Backup & Recovery O’Reilly Open Source Convention Jeremy Zawodny Yahoo! Finance July 24th, 2002.
Chapter 5 MYSQL Database. Introduction to MYSQL MySQL is the world's most popular open-source database. Open source means that the source code, the programming.
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting MySQL – Inserting Data.
MySQL More… 1. More on SQL In MySQL, the Information Schema is the “Catalog” in the SQL standard SQL has three components: Data definition Data manipulation.
Get Rid of Cron Scripts Using Events Sheeri Cabral Senior DB Admin/Architect,
Chapter 5 Files and Exceptions I. "The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. What is a file? A.
Oracle 9i. Agenda Start and exit SQL Plus (General) Start and exit SQL Plus (Tah 1006) Syntax Create a new user Create a new table Enter data into a new.
Relational Databases and MySQL. Relational Databases Relational databases model data by storing rows and columns in tables. The power of the relational.
Unit-8 Introduction Of MySql. Types of table in PHP MySQL supports various of table types or storage engines to allow you to optimize your database. The.
Best Practices in Loading Large Datasets Asanka Padmakumara (BSc,MCTS) SQL Server Sri Lanka User Group Meeting Oct 2013.
Introduction to MySQL  Working with MySQL and MySQL Workbench.
1 Backup and Replication Integration Techniques MySQL 6.0 Lars Thalmann, PhD Mats Kindahl, PhD Chuck Bell, PhD Replication and Backup Team Sun Microsystems.
Navigating MySQL Stored Procedures & Functions and Triggers Presented by: Sheeri K. Cabral At ODTUG Kaleidoscope 2010.
DBMS ● What are they? ● Why used ● Examples? – Oracle – Access – MySQL – Postgres – SQLServer – Sqlite.
Partitioning Sheeri K. Cabral Database Administrator The Pythian Group, January 12, 2009.
Sheeri Cabral Backing Up MySQL
A Better mysqltuner Sheeri K. Cabral The Pythian Group
3 A Guide to MySQL.
Welcome POS Synchronize Concept 08 Sept 2015.
Fundamentals of DBMS Notes-1.
CPSC-310 Database Systems
Would You Like Some Transactions With That Table?
Do-more Technical Training
Jonathan Walpole Computer Science Portland State University
Transactional Recovery and Checkpoints
MySQL-Database Jouni Juntunen Oulu University of Applied Sciences
Postgres MySQL Bakeoff
Introduction to MySQL.
Informatica PowerCenter Performance Tuning Tips
Applied CyberInfrastructure Concepts Fall 2017
LAB: Web-scale Data Management on a Cloud
Are You Getting the Best Out of Your MySQL Indexes?
Alejandro Álvarez on behalf of the FTS team
Database Design and Implementation
The Involuntary DBA Where there is Linux, you most likely will find MySQL or MariaDB. Like it or not, if you're working with Linux, you're a DBA.
Designing Tables for a Database System
MDEV Cross-Engine SQL Backup
Sergey Vojtovich Software MariaDB Foundation
ECONOMETRICS ii – spring 2018
DATABASE MANAGEMENT SYSTEM
Topics Introduction to File Input and Output
Interactive I/O Input from keyboard Must prompt user User friendly
Fundamentals of Databases
In Memory OLTP Not Just for OLTP.
Transmitter Interrupts
8 6 MySQL Special Topics A Guide to MySQL.
Stata Basic Course Lab 2.
Large Object Datatypes
SQL Basics BCHB697.
In Memory OLTP Not Just for OLTP.
Turn on spool and save to file a.txt
Topics Introduction to File Input and Output
Database Instructor: Bei Kang.
Lecture 20: Representing Data Elements
Presentation transcript:

Importing and Exporting Data with MySQL Presented by: Sheeri K. Cabral At ODTUG Kaleidoscope 2010

Who I Am MySQL DBA MySQL User Group First Oracle ACE Director for MySQL Lots of community stuff (videos, blog, podcast on hiatus)

How Universal import/export Storage engines Batched INSERTs LOAD DATA INFILE

Storage Engines Different ways of storing data Different load speeds How much does the technique matter? What else are you doing? If you have a storage engine that is just plain faster to load your data, it doesn't matter if you use a special technique or not. Also keep in mind: are you only bulk loading? Are you reading the table at the same time, or later? Are there updates? How frequently? Are you doing any transformations? Consider a “logging” table – if it's insert insert insert, there are tables that can optimize that. Is there a slave?

CSV storage engine Just what it sounds like No indexes No NULLs Drop-in table data – sort of CREATE TABLE music ( Name varchar(75) NOT NULL, Artist varchar(75) NOT NULL, Time_Sec SMALLINT UNSIGNED NOT NULL, Yr YEAR(4) NOT NULL, Date_Modified TIMESTAMP, Bit_Rate SMALLINT UNSIGNED NOT NULL, Sample_Rate INT UNSIGNED NOT NULL default 44100) ENGINE=CSV; INSERT INTO music (Name, Artist, Time_Sec, Yr, Date_Modified, Bit_Rate, Sample_Rate) VALUES ('Piano Man','Billy Joel',338, 1973,"2010_03_22 09:57:05",160,44100); mysql -u root -e "SHOW GLOBAL VARIABLES LIKE 'datadir';" exit dir music* mysql -u root test truncate music select * from music; copy "C:\Documents and Settings\cabral\Desktop\music.csv" . -- reload the table flush table music; CSV example: mysql -u root use test; CSV does not support indexes CREATE TABLE music ( Name varchar(75) NOT NULL, Artist varchar(75) NOT NULL, Time_Sec SMALLINT UNSIGNED default NULL, Yr YEAR(4) NOT NULL, Date_Modified TIMESTAMP, Bit_Rate SMALLINT UNSIGNED DEFAULT NULL, Sample_Rate INT UNSIGNED NOT NULL default 44100, INDEX (Artist,Name)) ENGINE=CSV; CSV does not support nulls Sample_Rate INT UNSIGNED NOT NULL default 44100)

MyISAM concurrent_insert=2 Key cache only, no data cache MyISAM – set concurrent_insert=2 for non-blocking data loading with INSERT 1 = concurrent inserts if there's no gaps (fragmentation), 2 = concurrent inserts even if there are gaps. MyISAM can delay index writes for no tables, tables created with the DELAY_KEY_WRITE option, or all tables. http://dev.mysql.com/doc/refman/5.1/en/create-table.html

InnoDB Consider the buffer pool Loads in memory first, then flushed to disk But data is then already in memory But MyISAM has an fs cache InnoDB worst to load because of the buffer pool But once it loads, it's already in memory, so better if you have lots of updates happening at the same time

Replication Topologies Different table type on the slave Loading directly to the slave Slave and master have different data! I won't go into these, but note that they exist

Regular INSERT statement mysql> INSERT INTO tbl (fld1, fld2,...) VALUES (val1,val2,...); Import a file $ mysql < file.sql with a mysql client command mysql> source file.sql

Batched INSERT statement INSERT INTO tbl (fld1, fld2...) VALUES (val1a, val2a...), (val1b, val2b), .... Faster than single inserts “Extended insert” Any storage engine

Other tricks for any storage engine Turn off foreign key checking Lock the table Build the indexes only at the end Other than extended insert.....

Turn Off Foreign Key Checking Only if you are 100% sure your data is good! SET SESSION FOREIGN_KEY_CHECKS=OFF; [load data] SET SESSION FOREIGN_KEY_CHECKS=ON; Does not EVER test loaded data! This can be dangerous

Lock the Table Other writes/reads may interfere with your load LOCK TABLES tbl WRITE; [load data] UNLOCK TABLES; Only do this if you really want to stop everything on this table though!

Build the Indexes at the End Disable keys, load, enable keys ALTER TABLE tbl DISABLE KEYS; [load data] ALTER TABLE tbl ENABLE KEYS; Wait as index builds all at once Extended insert is one trick to help things load faster no matter what the storage engine.

Exporting with mysqldump mysqldump commandline export tool by default: Extended insert (--skip-extended-insert) Disables keys (--skip-disable-keys) Locks the table for writes (--skip-add-locks) Drops the table and re-adds (--no-create-info -- skip-add-drop-table) Sets foreign key checking off Assumptive statements (--complete-insert) --opt is: --add-drop-table, --add-locks, --extended-insert, --disable-keys, --lock-tables, --create-options, --quick, --set-charset mysqldump --skip-opt -u root test music > "c:\Documents and Settings\cabral\music.sql" Edit "c:\Documents and Settings\cabral\music.sql" --skip-extended-insert –skip-disable-keys –skip-add-locks –skip-add-drop-table skip-add-locks Or --skip-opt –create-options –quick –set-charset –no-create-info –complete-insert mysqldump --skip-opt --no-create-info --complete-insert -u root test music > "c:\Documents and Settings\cabral\music.sql"

INSERT DELAYED Online batches with MyISAM, MEMORY, ARCHIVE, BLACKHOLE Delay queue Ignored on slaves http://dev.mysql.com/doc/refman/5.1/en/insert-delayed.html

INSERT DELAYED delayed_insert_limit 100 by default Handlers Terminates after waiting delayed_insert_limit 300 seconds by default delayed_queue_size 1000 by default This is a per-table limit on the number of rows to queue when handling INSERT DELAYED statements. If the queue becomes full, any client that issues an INSERT DELAYED statement waits until there is room in the queue again. http://dev.mysql.com/doc/refman/5.1/en/insert-delayed.html

Delayed Key Write MyISAM only Index buffers not flushed after each write Flushed (keys written) only on table close FLUSH TABLES closes and re-opens Only for speeding up writes In general, not a good idea

DELAY_KEY_WRITE delay_key_write system variable CREATE TABLE … () ENGINE=MYISAM DELAY_KEY_WRITE=1; delay_key_write system variable ON by default, honors table definition OFF is never delay key writes ALL is always delay key writes

LOAD DATA INFILE Table must already exist Table name does not need to relate to file name: mysql> LOAD DATA INFILE '/tmp/importme.sql' INTO TABLE rental; Complement of SELECT INTO OUTFILE More details on future slides...

LOAD DATA INFILE On an empty MyISAM table, non-unique indexes are created in a separate batch Possibly made even faster by ALTER TABLE tbl DISABLE KEYS LOAD DATA INFILE... ALTER TABLE tbl ENABLE KEYS

LOAD DATA INFILE LOAD DATA [LOW_PRIORITY | CONCURRENT] less/no blocking [LOCAL] from client machine INFILE 'file_name' can be full path

LOAD DATA INFILE LOAD DATA INFILE 'file_name' [REPLACE | IGNORE] What to do if there are duplicates Default is error INTO TABLE tbl_name

LOAD DATA INFILE LOAD DATA INFILE 'file_name' INTO TABLE tbl_name [CHARACTER SET charset_name] CREATE TABLE music ( Name varchar(75) NOT NULL, Artist varchar(75) NOT NULL, Time_Sec SMALLINT UNSIGNED NOT NULL, Yr YEAR(4) NOT NULL, Date_Modified TIMESTAMP, Bit_Rate SMALLINT UNSIGNED NOT NULL, Sample_Rate INT UNSIGNED NOT NULL default 44100) ENGINE=MyISAM; LOAD DATA INFILE 'C:\\Documents and Settings\\cabral\\Desktop\\music.csv' INTO TABLE music FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '”';

LOAD DATA INFILE LOAD DATA INFILE 'file_name' INTO TABLE tbl_name [FIELDS [TERMINATED BY 'string'] (comma) [[OPTIONALLY] ENCLOSED BY 'char'] [ESCAPED BY 'char']] [LINES [STARTING BY 'string'] [TERMINATED BY 'string']] (newline) CREATE TABLE music ( Name varchar(75) NOT NULL, Artist varchar(75) NOT NULL, Time_Sec SMALLINT UNSIGNED NOT NULL, Yr YEAR(4) NOT NULL, Date_Modified TIMESTAMP, Bit_Rate SMALLINT UNSIGNED NOT NULL, Sample_Rate INT UNSIGNED NOT NULL default 44100) ENGINE=MyISAM; LOAD DATA INFILE 'C:\\Documents and Settings\\cabral\\Desktop\\music.csv' INTO TABLE music FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '”';

SELECT INTO OUTFILE SELECT * INTO OUTFILE 'file_name' [CHARACTER SET charset_name] [FIELDS [TERMINATED BY 'string'] [[OPTIONALLY] ENCLOSED BY 'char'] [ESCAPED BY 'char']] [LINES [STARTING BY 'string'] [TERMINATED BY 'string']]

SELECT INTO DUMPFILE SELECT * INTO DUMPFILE 'file_name'; No other options Good for BLOBs

Faster inserts/imports Batched inserts Lock tables Turn off foreign key checking Build index at end of import DELAY_KEY_WRITE INSERT DELAYED

Import/export Import LOAD DATA INFILE Different storage engines (ie, CSV) Export mysqldump SELECT … INTO OUTFILE SELECT … INTO DUMPFILE