SlideShare ist ein Scribd-Unternehmen logo
1 von 28
Downloaden Sie, um offline zu lesen
DATABASE/SQL
• github.com/VividCortex
• @xaprb
• baron@vividcortex.com
• linkedin.com/in/xaprb
Optimization, Backups,
Replication, and more
Baron Schwartz,
Peter Zaitsev &
Vadim Tkachenko
High
Performance
MySQL
3rd
Edition
CoversVersion
5.5
ME
• Docs: golang.org/pkg/database/sql/
• Tutorial: go-database-sql.org/
• MySQL Driver: github.com/go-sql-driver/mysql/
RESOURCES
THINGS
DB
• Not a connection.
• Open() Close()
• Query() QueryRow() Exec()
ROWS
• Next()
• Scan()
PATTERNS
package main
import (
! _ "github.com/go-sql-driver/mysql"
! "database/sql"
! "log"
)
func main() {
! db, err := sql.Open("mysql",
! ! "user:password@tcp(127.0.0.1:3306)/test")
! if err != nil {
! ! log.Println(err)
! }
! // What do we have here?
! defer db.Close()
}
var str string
err = db.QueryRow(
"select hello from world where id = 1").
Scan(&str)
if err != nil && err != sql.ErrNoRows {
log.Println(err)
}
log.Println(str)
q := "select id, hello from world where id = ?"
rows, err := db.Query(q, 1)
if err != nil {
! log.Fatal(err)
}
defer rows.Close()
var id int
var str string
for rows.Next() {
! err = rows.Scan(&id, &str)
! if err != nil {
! ! log.Fatal(err)
! }
! // Use the variables scanned from the row
}
for rows.Next() {
! // scan
}
if err = rows.Err(); err != nil {
rows.Close()
log.Fatal(err)
}
stmt, err := db.Prepare(
! "select id, hello from world where id = ?")
if err != nil {
! log.Fatal(err)
}
defer stmt.Close()
rows, err := stmt.Query(1)
if err != nil {
! log.Fatal(err)
}
defer rows.Close()
for rows.Next() {
! // ...
}
stmt, err := db.Prepare(
! "insert into world(hello) values(?)")
if err != nil {
! log.Fatal(err)
}
defer stmt.Close()
res, err := stmt.Exec("hello, Dolly")
if err != nil {
! log.Fatal(err)
}
THINGSTO KNOW
THINGSTO KNOW
• There is a connection pool.
• You can’t use uint64 with high bit set in parameters.
DON’T BE LAZY
// Don’t do this!
for i := 0; i < 50; i++ {
! _, err := db.Query("DELETE FROM hello.world")
}
// Use this instead!
for i := 0; i < 50; i++ {
! _, err := db.Exec("DELETE FROM hello.world")
}
MORE HELPFUL ADVICE
• Don’t defer() in long-running functions.
• Don’t use cxn state. UseTx to bind to a connection.
• Don’t use BEGIN and COMMIT via SQL.
SURPRISES
RETRY. RETRY. RETRY. RETRY. RETRY.
RETRY. RETRY. RETRY. RETRY. RETRY.
// Exec executes a query without returning any
rows.
// The args are for any placeholder parameters in
the query.
func (db *DB) Exec(query string,
args ...interface{}) (Result, error) {
! var res Result
! var err error
! for i := 0; i < 10; i++ {
! ! res, err = db.exec(query, args)
! ! if err != driver.ErrBadConn {
! ! ! break
! ! }
! }
! return res, err
}
PREPARED STATEMENTS
NULL
This page intentionally left blank.
var s sql.NullString
err := db.QueryRow(
"SELECT name FROM foo WHERE id=?",
id).Scan(&s)
if s.Valid {
// use s.String
} else {
// NULL value
}
CUSTOMTYPES
CUSTOMTYPES
• You can implement theValuer and Scanner interfaces.
• Why?Transparently transform data in <=> out of the DB.
• Compress and uncompress.
• Marshall/unmarshall JSON or another type.
• Encrypt and decrypt.
• See e.g. http://jmoiron.net/blog/built-in-interfaces/
RESOURCES
• http://golang.org/pkg/database/sql/
• http://go-database-sql.org/
• https://github.com/go-sql-driver/mysql
• http://jmoiron.net/blog/
• @VividCortex * not biased, not biased, not biased
IMAGE CREDITS
• http://www.flickr.com/photos/simens/6306917636/
• http://www.flickr.com/photos/dexxus/5794905716/
• http://www.flickr.com/photos/sebastian_bergmann/202396633/
• http://www.flickr.com/photos/doug88888/4794114114/
• http://www.flickr.com/photos/oatsy40/6443878013/
• http://www.sxc.hu/photo/1160562/
• Google Maps (screenshot)
• http://www.flickr.com/photos/estherase/13553883/
• http://www.flickr.com/photos/paperpariah/4150220583/
• http://www.flickr.com/photos/zooboing/4743616313/
• http://www.flickr.com/photos/dseneste/5912382808/
• http://www.flickr.com/photos/clickykbd/66165381/sizes/l/
• http://www.flickr.com/photos/mamnaimie/5576980406/
• https://www.flickr.com/photos/zachstern/87431231

Weitere ähnliche Inhalte

Was ist angesagt?

Celery - A Distributed Task Queue
Celery - A Distributed Task QueueCelery - A Distributed Task Queue
Celery - A Distributed Task QueueDuy Do
 
Why Task Queues - ComoRichWeb
Why Task Queues - ComoRichWebWhy Task Queues - ComoRichWeb
Why Task Queues - ComoRichWebBryan Helmig
 
Europython 2011 - Playing tasks with Django & Celery
Europython 2011 - Playing tasks with Django & CeleryEuropython 2011 - Playing tasks with Django & Celery
Europython 2011 - Playing tasks with Django & CeleryMauro Rocco
 
Reliable Python REST API (by Volodymyr Hotsyk) - Web Back-End Tech Hangout - ...
Reliable Python REST API (by Volodymyr Hotsyk) - Web Back-End Tech Hangout - ...Reliable Python REST API (by Volodymyr Hotsyk) - Web Back-End Tech Hangout - ...
Reliable Python REST API (by Volodymyr Hotsyk) - Web Back-End Tech Hangout - ...Innovecs
 
Web Crawling with NodeJS
Web Crawling with NodeJSWeb Crawling with NodeJS
Web Crawling with NodeJSSylvain Zimmer
 
Scaling up task processing with Celery
Scaling up task processing with CeleryScaling up task processing with Celery
Scaling up task processing with CeleryNicolas Grasset
 
MongoDB: tips, trick and hacks
MongoDB: tips, trick and hacksMongoDB: tips, trick and hacks
MongoDB: tips, trick and hacksScott Hernandez
 
Got Logs? Get Answers with Elasticsearch ELK - PuppetConf 2014
Got Logs? Get Answers with Elasticsearch ELK - PuppetConf 2014Got Logs? Get Answers with Elasticsearch ELK - PuppetConf 2014
Got Logs? Get Answers with Elasticsearch ELK - PuppetConf 2014Puppet
 
Django Celery - A distributed task queue
Django Celery - A distributed task queueDjango Celery - A distributed task queue
Django Celery - A distributed task queueAlex Eftimie
 
Performance and stability testing \w Gatling
Performance and stability testing \w GatlingPerformance and stability testing \w Gatling
Performance and stability testing \w GatlingDmitry Vrublevsky
 
Introduction to PostgreSQL
Introduction to PostgreSQLIntroduction to PostgreSQL
Introduction to PostgreSQLJoel Brewer
 
Selenium sandwich-3: Being where you aren't.
Selenium sandwich-3: Being where you aren't.Selenium sandwich-3: Being where you aren't.
Selenium sandwich-3: Being where you aren't.Workhorse Computing
 
My app is secure... I think
My app is secure... I thinkMy app is secure... I think
My app is secure... I thinkWim Godden
 
Real-time search in Drupal. Meet Elasticsearch
Real-time search in Drupal. Meet ElasticsearchReal-time search in Drupal. Meet Elasticsearch
Real-time search in Drupal. Meet ElasticsearchAlexei Gorobets
 
Ember background basics
Ember background basicsEmber background basics
Ember background basicsPhilipp Fehre
 
Beyond PHP - it's not (just) about the code
Beyond PHP - it's not (just) about the codeBeyond PHP - it's not (just) about the code
Beyond PHP - it's not (just) about the codeWim Godden
 
Activator and Reactive at Play NYC meetup
Activator and Reactive at Play NYC meetupActivator and Reactive at Play NYC meetup
Activator and Reactive at Play NYC meetupHenrik Engström
 
When dynamic becomes static: the next step in web caching techniques
When dynamic becomes static: the next step in web caching techniquesWhen dynamic becomes static: the next step in web caching techniques
When dynamic becomes static: the next step in web caching techniquesWim Godden
 
Stubる - Mockingjayを使ったHTTPクライアントのテスト -
Stubる - Mockingjayを使ったHTTPクライアントのテスト -Stubる - Mockingjayを使ったHTTPクライアントのテスト -
Stubる - Mockingjayを使ったHTTPクライアントのテスト -Kenji Tanaka
 
Asynchronous Task Queues with Celery
Asynchronous Task Queues with CeleryAsynchronous Task Queues with Celery
Asynchronous Task Queues with CeleryKishor Kumar
 

Was ist angesagt? (20)

Celery - A Distributed Task Queue
Celery - A Distributed Task QueueCelery - A Distributed Task Queue
Celery - A Distributed Task Queue
 
Why Task Queues - ComoRichWeb
Why Task Queues - ComoRichWebWhy Task Queues - ComoRichWeb
Why Task Queues - ComoRichWeb
 
Europython 2011 - Playing tasks with Django & Celery
Europython 2011 - Playing tasks with Django & CeleryEuropython 2011 - Playing tasks with Django & Celery
Europython 2011 - Playing tasks with Django & Celery
 
Reliable Python REST API (by Volodymyr Hotsyk) - Web Back-End Tech Hangout - ...
Reliable Python REST API (by Volodymyr Hotsyk) - Web Back-End Tech Hangout - ...Reliable Python REST API (by Volodymyr Hotsyk) - Web Back-End Tech Hangout - ...
Reliable Python REST API (by Volodymyr Hotsyk) - Web Back-End Tech Hangout - ...
 
Web Crawling with NodeJS
Web Crawling with NodeJSWeb Crawling with NodeJS
Web Crawling with NodeJS
 
Scaling up task processing with Celery
Scaling up task processing with CeleryScaling up task processing with Celery
Scaling up task processing with Celery
 
MongoDB: tips, trick and hacks
MongoDB: tips, trick and hacksMongoDB: tips, trick and hacks
MongoDB: tips, trick and hacks
 
Got Logs? Get Answers with Elasticsearch ELK - PuppetConf 2014
Got Logs? Get Answers with Elasticsearch ELK - PuppetConf 2014Got Logs? Get Answers with Elasticsearch ELK - PuppetConf 2014
Got Logs? Get Answers with Elasticsearch ELK - PuppetConf 2014
 
Django Celery - A distributed task queue
Django Celery - A distributed task queueDjango Celery - A distributed task queue
Django Celery - A distributed task queue
 
Performance and stability testing \w Gatling
Performance and stability testing \w GatlingPerformance and stability testing \w Gatling
Performance and stability testing \w Gatling
 
Introduction to PostgreSQL
Introduction to PostgreSQLIntroduction to PostgreSQL
Introduction to PostgreSQL
 
Selenium sandwich-3: Being where you aren't.
Selenium sandwich-3: Being where you aren't.Selenium sandwich-3: Being where you aren't.
Selenium sandwich-3: Being where you aren't.
 
My app is secure... I think
My app is secure... I thinkMy app is secure... I think
My app is secure... I think
 
Real-time search in Drupal. Meet Elasticsearch
Real-time search in Drupal. Meet ElasticsearchReal-time search in Drupal. Meet Elasticsearch
Real-time search in Drupal. Meet Elasticsearch
 
Ember background basics
Ember background basicsEmber background basics
Ember background basics
 
Beyond PHP - it's not (just) about the code
Beyond PHP - it's not (just) about the codeBeyond PHP - it's not (just) about the code
Beyond PHP - it's not (just) about the code
 
Activator and Reactive at Play NYC meetup
Activator and Reactive at Play NYC meetupActivator and Reactive at Play NYC meetup
Activator and Reactive at Play NYC meetup
 
When dynamic becomes static: the next step in web caching techniques
When dynamic becomes static: the next step in web caching techniquesWhen dynamic becomes static: the next step in web caching techniques
When dynamic becomes static: the next step in web caching techniques
 
Stubる - Mockingjayを使ったHTTPクライアントのテスト -
Stubる - Mockingjayを使ったHTTPクライアントのテスト -Stubる - Mockingjayを使ったHTTPクライアントのテスト -
Stubる - Mockingjayを使ったHTTPクライアントのテスト -
 
Asynchronous Task Queues with Celery
Asynchronous Task Queues with CeleryAsynchronous Task Queues with Celery
Asynchronous Task Queues with Celery
 

Andere mochten auch

Fraser Graham Killer Robots
Fraser Graham Killer RobotsFraser Graham Killer Robots
Fraser Graham Killer RobotsArtem Kovardin
 
High Performance Systems in Go - GopherCon 2014
High Performance Systems in Go - GopherCon 2014High Performance Systems in Go - GopherCon 2014
High Performance Systems in Go - GopherCon 2014Derek Collison
 
A Channel Compendium
A Channel CompendiumA Channel Compendium
A Channel CompendiumCloudflare
 
Building A Relevancy Engine Using MongoDB and Go
Building A Relevancy Engine Using MongoDB and GoBuilding A Relevancy Engine Using MongoDB and Go
Building A Relevancy Engine Using MongoDB and Goardan-bkennedy
 
Best practices-for-production-environments
Best practices-for-production-environmentsBest practices-for-production-environments
Best practices-for-production-environmentsArtem Kovardin
 
Garbage collector и управление памятью в Go
Garbage collector и управление памятью в GoGarbage collector и управление памятью в Go
Garbage collector и управление памятью в GoArtem Kovardin
 
C-spirit reborn: why Go was bound to be created
C-spirit reborn: why Go was bound to be createdC-spirit reborn: why Go was bound to be created
C-spirit reborn: why Go was bound to be createdArtem Kovardin
 
Talknoteとgolangと私
Talknoteとgolangと私Talknoteとgolangと私
Talknoteとgolangと私Kenyu Miura
 
GoによるWebアプリ開発のキホン
GoによるWebアプリ開発のキホンGoによるWebアプリ開発のキホン
GoによるWebアプリ開発のキホンAkihiko Horiuchi
 

Andere mochten auch (11)

Fraser Graham Killer Robots
Fraser Graham Killer RobotsFraser Graham Killer Robots
Fraser Graham Killer Robots
 
High Performance Systems in Go - GopherCon 2014
High Performance Systems in Go - GopherCon 2014High Performance Systems in Go - GopherCon 2014
High Performance Systems in Go - GopherCon 2014
 
A Channel Compendium
A Channel CompendiumA Channel Compendium
A Channel Compendium
 
Building A Relevancy Engine Using MongoDB and Go
Building A Relevancy Engine Using MongoDB and GoBuilding A Relevancy Engine Using MongoDB and Go
Building A Relevancy Engine Using MongoDB and Go
 
Best practices-for-production-environments
Best practices-for-production-environmentsBest practices-for-production-environments
Best practices-for-production-environments
 
Garbage collector и управление памятью в Go
Garbage collector и управление памятью в GoGarbage collector и управление памятью в Go
Garbage collector и управление памятью в Go
 
C-spirit reborn: why Go was bound to be created
C-spirit reborn: why Go was bound to be createdC-spirit reborn: why Go was bound to be created
C-spirit reborn: why Go was bound to be created
 
Anthony Starks - deck
Anthony Starks - deckAnthony Starks - deck
Anthony Starks - deck
 
OOP в Go
OOP в GoOOP в Go
OOP в Go
 
Talknoteとgolangと私
Talknoteとgolangと私Talknoteとgolangと私
Talknoteとgolangと私
 
GoによるWebアプリ開発のキホン
GoによるWebアプリ開発のキホンGoによるWebアプリ開発のキホン
GoによるWebアプリ開発のキホン
 

Ähnlich wie SQL Database and Golang

Scrap your query boilerplate with specql
Scrap your query boilerplate with specqlScrap your query boilerplate with specql
Scrap your query boilerplate with specqlTatu Tarvainen
 
Java OOP Programming language (Part 8) - Java Database JDBC
Java OOP Programming language (Part 8) - Java Database JDBCJava OOP Programming language (Part 8) - Java Database JDBC
Java OOP Programming language (Part 8) - Java Database JDBCOUM SAOKOSAL
 
How to Troubleshoot & Optimize Database Query Performance for Your Application
How to Troubleshoot  & Optimize Database Query Performance for Your ApplicationHow to Troubleshoot  & Optimize Database Query Performance for Your Application
How to Troubleshoot & Optimize Database Query Performance for Your ApplicationDynatrace
 
Solid And Sustainable Development in Scala
Solid And Sustainable Development in ScalaSolid And Sustainable Development in Scala
Solid And Sustainable Development in ScalaKazuhiro Sera
 
Ruby on Rails Oracle adaptera izstrāde
Ruby on Rails Oracle adaptera izstrādeRuby on Rails Oracle adaptera izstrāde
Ruby on Rails Oracle adaptera izstrādeRaimonds Simanovskis
 
Solid and Sustainable Development in Scala
Solid and Sustainable Development in ScalaSolid and Sustainable Development in Scala
Solid and Sustainable Development in Scalascalaconfjp
 
Different waysconnect
Different waysconnectDifferent waysconnect
Different waysconnectmyrajendra
 
Building node.js applications with Database Jones
Building node.js applications with Database JonesBuilding node.js applications with Database Jones
Building node.js applications with Database JonesJohn David Duncan
 
ABRIDGED VERSION - Joys & frustrations of putting 34,000 lines of Haskell in...
 ABRIDGED VERSION - Joys & frustrations of putting 34,000 lines of Haskell in... ABRIDGED VERSION - Joys & frustrations of putting 34,000 lines of Haskell in...
ABRIDGED VERSION - Joys & frustrations of putting 34,000 lines of Haskell in...Saurabh Nanda
 
Express Presentation
Express PresentationExpress Presentation
Express Presentationaaronheckmann
 
MYSQL Query Anti-Patterns That Can Be Moved to Sphinx
MYSQL Query Anti-Patterns That Can Be Moved to SphinxMYSQL Query Anti-Patterns That Can Be Moved to Sphinx
MYSQL Query Anti-Patterns That Can Be Moved to SphinxPythian
 

Ähnlich wie SQL Database and Golang (20)

SQL Injection Defense in Python
SQL Injection Defense in PythonSQL Injection Defense in Python
SQL Injection Defense in Python
 
Python with MySql.pptx
Python with MySql.pptxPython with MySql.pptx
Python with MySql.pptx
 
Scrap your query boilerplate with specql
Scrap your query boilerplate with specqlScrap your query boilerplate with specql
Scrap your query boilerplate with specql
 
Java OOP Programming language (Part 8) - Java Database JDBC
Java OOP Programming language (Part 8) - Java Database JDBCJava OOP Programming language (Part 8) - Java Database JDBC
Java OOP Programming language (Part 8) - Java Database JDBC
 
Es6 hackathon
Es6 hackathonEs6 hackathon
Es6 hackathon
 
How to Troubleshoot & Optimize Database Query Performance for Your Application
How to Troubleshoot  & Optimize Database Query Performance for Your ApplicationHow to Troubleshoot  & Optimize Database Query Performance for Your Application
How to Troubleshoot & Optimize Database Query Performance for Your Application
 
Solid And Sustainable Development in Scala
Solid And Sustainable Development in ScalaSolid And Sustainable Development in Scala
Solid And Sustainable Development in Scala
 
Ruby on Rails Oracle adaptera izstrāde
Ruby on Rails Oracle adaptera izstrādeRuby on Rails Oracle adaptera izstrāde
Ruby on Rails Oracle adaptera izstrāde
 
Jdbc
JdbcJdbc
Jdbc
 
Python database access
Python database accessPython database access
Python database access
 
Solid and Sustainable Development in Scala
Solid and Sustainable Development in ScalaSolid and Sustainable Development in Scala
Solid and Sustainable Development in Scala
 
Different waysconnect
Different waysconnectDifferent waysconnect
Different waysconnect
 
Building node.js applications with Database Jones
Building node.js applications with Database JonesBuilding node.js applications with Database Jones
Building node.js applications with Database Jones
 
Jdbc
JdbcJdbc
Jdbc
 
Java JDBC
Java JDBCJava JDBC
Java JDBC
 
ABRIDGED VERSION - Joys & frustrations of putting 34,000 lines of Haskell in...
 ABRIDGED VERSION - Joys & frustrations of putting 34,000 lines of Haskell in... ABRIDGED VERSION - Joys & frustrations of putting 34,000 lines of Haskell in...
ABRIDGED VERSION - Joys & frustrations of putting 34,000 lines of Haskell in...
 
Scala active record
Scala active recordScala active record
Scala active record
 
Express Presentation
Express PresentationExpress Presentation
Express Presentation
 
MYSQL Query Anti-Patterns That Can Be Moved to Sphinx
MYSQL Query Anti-Patterns That Can Be Moved to SphinxMYSQL Query Anti-Patterns That Can Be Moved to Sphinx
MYSQL Query Anti-Patterns That Can Be Moved to Sphinx
 
PHP with MySQL
PHP with MySQLPHP with MySQL
PHP with MySQL
 

Kürzlich hochgeladen

What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfMounikaPolabathina
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxLoriGlavin3
 
unit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxunit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxBkGupta21
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
What is Artificial Intelligence?????????
What is Artificial Intelligence?????????What is Artificial Intelligence?????????
What is Artificial Intelligence?????????blackmambaettijean
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfLoriGlavin3
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxLoriGlavin3
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.Curtis Poe
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxhariprasad279825
 
Training state-of-the-art general text embedding
Training state-of-the-art general text embeddingTraining state-of-the-art general text embedding
Training state-of-the-art general text embeddingZilliz
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersNicole Novielli
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024Lonnie McRorey
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity PlanDatabarracks
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxLoriGlavin3
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionDilum Bandara
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningLars Bell
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
Time Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsTime Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsNathaniel Shimoni
 

Kürzlich hochgeladen (20)

What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdf
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
 
unit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxunit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptx
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
What is Artificial Intelligence?????????
What is Artificial Intelligence?????????What is Artificial Intelligence?????????
What is Artificial Intelligence?????????
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdf
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptx
 
Training state-of-the-art general text embedding
Training state-of-the-art general text embeddingTraining state-of-the-art general text embedding
Training state-of-the-art general text embedding
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software Developers
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity Plan
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An Introduction
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine Tuning
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
Time Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsTime Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directions
 

SQL Database and Golang

  • 2. • github.com/VividCortex • @xaprb • baron@vividcortex.com • linkedin.com/in/xaprb Optimization, Backups, Replication, and more Baron Schwartz, Peter Zaitsev & Vadim Tkachenko High Performance MySQL 3rd Edition CoversVersion 5.5 ME
  • 3. • Docs: golang.org/pkg/database/sql/ • Tutorial: go-database-sql.org/ • MySQL Driver: github.com/go-sql-driver/mysql/ RESOURCES
  • 5. DB • Not a connection. • Open() Close() • Query() QueryRow() Exec()
  • 8. package main import ( ! _ "github.com/go-sql-driver/mysql" ! "database/sql" ! "log" ) func main() { ! db, err := sql.Open("mysql", ! ! "user:password@tcp(127.0.0.1:3306)/test") ! if err != nil { ! ! log.Println(err) ! } ! // What do we have here? ! defer db.Close() }
  • 9. var str string err = db.QueryRow( "select hello from world where id = 1"). Scan(&str) if err != nil && err != sql.ErrNoRows { log.Println(err) } log.Println(str)
  • 10. q := "select id, hello from world where id = ?" rows, err := db.Query(q, 1) if err != nil { ! log.Fatal(err) } defer rows.Close() var id int var str string for rows.Next() { ! err = rows.Scan(&id, &str) ! if err != nil { ! ! log.Fatal(err) ! } ! // Use the variables scanned from the row }
  • 11. for rows.Next() { ! // scan } if err = rows.Err(); err != nil { rows.Close() log.Fatal(err) }
  • 12. stmt, err := db.Prepare( ! "select id, hello from world where id = ?") if err != nil { ! log.Fatal(err) } defer stmt.Close() rows, err := stmt.Query(1) if err != nil { ! log.Fatal(err) } defer rows.Close() for rows.Next() { ! // ... }
  • 13. stmt, err := db.Prepare( ! "insert into world(hello) values(?)") if err != nil { ! log.Fatal(err) } defer stmt.Close() res, err := stmt.Exec("hello, Dolly") if err != nil { ! log.Fatal(err) }
  • 15. THINGSTO KNOW • There is a connection pool. • You can’t use uint64 with high bit set in parameters.
  • 17. // Don’t do this! for i := 0; i < 50; i++ { ! _, err := db.Query("DELETE FROM hello.world") } // Use this instead! for i := 0; i < 50; i++ { ! _, err := db.Exec("DELETE FROM hello.world") }
  • 18. MORE HELPFUL ADVICE • Don’t defer() in long-running functions. • Don’t use cxn state. UseTx to bind to a connection. • Don’t use BEGIN and COMMIT via SQL.
  • 20. RETRY. RETRY. RETRY. RETRY. RETRY. RETRY. RETRY. RETRY. RETRY. RETRY.
  • 21. // Exec executes a query without returning any rows. // The args are for any placeholder parameters in the query. func (db *DB) Exec(query string, args ...interface{}) (Result, error) { ! var res Result ! var err error ! for i := 0; i < 10; i++ { ! ! res, err = db.exec(query, args) ! ! if err != driver.ErrBadConn { ! ! ! break ! ! } ! } ! return res, err }
  • 24. var s sql.NullString err := db.QueryRow( "SELECT name FROM foo WHERE id=?", id).Scan(&s) if s.Valid { // use s.String } else { // NULL value }
  • 26. CUSTOMTYPES • You can implement theValuer and Scanner interfaces. • Why?Transparently transform data in <=> out of the DB. • Compress and uncompress. • Marshall/unmarshall JSON or another type. • Encrypt and decrypt. • See e.g. http://jmoiron.net/blog/built-in-interfaces/
  • 27. RESOURCES • http://golang.org/pkg/database/sql/ • http://go-database-sql.org/ • https://github.com/go-sql-driver/mysql • http://jmoiron.net/blog/ • @VividCortex * not biased, not biased, not biased
  • 28. IMAGE CREDITS • http://www.flickr.com/photos/simens/6306917636/ • http://www.flickr.com/photos/dexxus/5794905716/ • http://www.flickr.com/photos/sebastian_bergmann/202396633/ • http://www.flickr.com/photos/doug88888/4794114114/ • http://www.flickr.com/photos/oatsy40/6443878013/ • http://www.sxc.hu/photo/1160562/ • Google Maps (screenshot) • http://www.flickr.com/photos/estherase/13553883/ • http://www.flickr.com/photos/paperpariah/4150220583/ • http://www.flickr.com/photos/zooboing/4743616313/ • http://www.flickr.com/photos/dseneste/5912382808/ • http://www.flickr.com/photos/clickykbd/66165381/sizes/l/ • http://www.flickr.com/photos/mamnaimie/5576980406/ • https://www.flickr.com/photos/zachstern/87431231