mysql

mysql: using json data and not hating it

it’s a universal truth that one of the primary jobs of backend developers is drinking from the firehose of json the frontends are constantly spraying at us. normally, we pick apart all those key/value pairs and slot them into the neatly-arranged columns of our db, keeping our database structure nice and normal. but sometimes there are good reasons to just store a whole json object or array as an undifferentiated blob.

in the dark, old days, this json would go in a text column, which was fine if all we needed to do was store it and return it. but if we needed to do something more complex like, extract certain values from that json or, god forbid, use one in a WHERE clause, things could get out of hand pretty quickly.

fortunately, modern mysql supports JSON as a native data type and offers a whole host of useful json functions, allowing us to work with json data in a way that’s almost pleasurable. in this post we’re going to go over extracting values from json and using json data in WHERE clauses.

this column can fit so much schema-less data in it
Continue reading →
Posted by grant horwood in mysql, 0 comments

mysql: using the slow query log

the cold, hard truth is that the reason your web app or api is running slow is because of your database calls. you can migrate everything to frakenphp or put compression in your http server if you want, i’m not going to stop you, but if you want to shave whole seconds off your response times, you should take a hard look at all that janky sql and yolo orm queries you wrote.

the good news is that mysql has a tool to find and log your slow queries. it’s called the ‘slow query log’, basically the perfect name, and it comes pre-installed. let’s get it turned on, set up, and look at the output.

a developer discovering they’re really bad at writing sql
Continue reading →
Posted by grant horwood in mysql, 0 comments

making mysql STDOUT less (or more) of a mess

you’re doing some command line mysql work, something like piping in a query to mysql and dumping the output to STDOUT. what you want, of course, is some nicely-formatted output. what you get, instead, is hot trash like this:

$ echo "SELECT * FROM albums" | mysql record_collection_db
id  artist  tite
1   Bratmobile  Pottymouth
2   Coltrane, John  Giant Steps

fortunately, mysql has some built-in formatters to help the world make sense again.

Continue reading →
Posted by grant horwood in mysql