Showing posts with label Mysql Blackhole. Show all posts
Showing posts with label Mysql Blackhole. Show all posts

Friday, 9 November 2018

Mysql: Is a 'blackhole' table evil?

Reading to this question i've just learned the existence of the blackhole table trick: basically consist in using a single table to insert data, and then a trigger that split the data in many other tables.
Im wondering if this could cause problems, once the developers whos working on the project are aware of that.
What are the pro and cons of this tecnique?
Edit: The blink I got in mind when I saw the example, is about transactions: if for some reason the transaction fail, you'll find the blackhole row with the original data, for historical purpose and maybe a help with debug - but this seems to be the only +1 i can see with blackholes. Ideas?

 Answers


I don't think blackhole has any real pros.
Writing the trigger code to move data around is probably not noticably less work than writing the code to insert the data in the right place in the first place.
As Christian Oudard writes, it doesn't reduce complexity - just moves it to a place where it's really hard to debug.
On the downside:
"Side effects" are usually a bad idea in software development. Triggers are side effects - I intend to do one thing (insert data in a table), and it actually does lots of other things. Now, when I'm debugging my code, I have to keep all the side effects in my head too - and the side effects could themselves have side effects.
most software spends far more time in maintenance than it does in development. Bringing new developers into the team and explaining the black hole trick is likely to increase the learning curve - for negligible benefit (in my view).
Because triggers are side effects, and it's relatively easy to set off a huge cascade of triggers if you're not careful, I've always tried to design my databases without a reliance on triggers; where triggers are clearly the right way to go, I've only let my most experienced developers create them. The black hole trick makes triggers into a normal, regular way of working. This is a personal point of view, of course.



This doesn't look like a good idea. If you're trying to keep the front end code simple, why not just use a stored procedure? If it's not to keep the front end code simple, I don't understand the point at all.



Each time you insert a row into a table, the odds are that you are writing to the same area of the hard drive or the same page (in MS-SQL world, I don't know about postgresql), so this technique will likely lead to contention and locking as all transactions are now competing to write to the same table.
Also this will halve insert performance since inserts require two inserts instead of one.
And this is denormalization since there are now two copies of the data instead of one.



Don't do this. The fact that it's called a trick and not a standard way of doing something says enough for me.
This totally kills the normal usage pattern of the relational model. Not sure that it actually kills normal form as you can still have that all in place. It's just messing with the way data is making it to the destination tables. Looks like a performance nightmare on top of a maintenance nightmare. Imagine one table having a trigger that has to fire for 1,800 plus table inserts for example. That just makes me feel sick.
This is a interesting parlor trick nothing more.

What's the equivalent of blackhole?

I'm starting a new project in postgreSQL (lastest) having worked with MySQL 5.5 for a while.
In the past I've made heavy use of blackhole table to simplify my application code.
It allows me to do one insert in application code:
INSERT INTO blackhole1 (val1, val2, val3, val4 ...

CREATE TRIGGER ai_blackhole1_each AFTER INSERT ....
BEGIN
  INSERT INTO table1 (....
  INSERT INTO table2 (....
  INSERT INTO log (.....
And have a trigger in the blackhole table insert the values into different tables.
What do I use in postgreSQL to replace this functionality?
I know I can use a stored procedure, but that means that I cannot connect data-aware controls to a 
blackhole-table. So I would love the stick as close to the MySQL original as possible.

 Answers


What is a BLACKHOLE?
In MySQL-speak, BLACKHOLE is a storage engine that simply discards all data INSERTed into it, analogous to a null device. There are a number of reasons to use this backend, but they tend to be a bit abstruse:
  • A "relay-only" binlog-filtering slave
    See the docs, and here.
  • Benchmarking
    E.g., measuring the overhead of binary logging without worrying about storage engine overhead
If you don't know why you need a data sink masquerading as a table, don't use it.

What is the technique you are asking about?

The use under consideration seems to be to:
  1. redirect INSERTed data to other tables
  2. audit log the original INSERTion action
  3. discard the original INSERT data
Thus the answer to the question of "evilness" or pros/cons is the same as the answer to those questions for insertable/updatable VIEWs (the common way to implement #1), trigger-based audit logging (how most people do #2) and behavioral overrides/counteractions generally (there are a number of ways to accomplish #3).

So, what is the answer?

The answer is, of course, "sometimes these techniques are appropriate and sometimes not." :) Do you know why you're doing it? Is the application a better place for this functionality? Is the abstraction too brittle, too leaky, too rigid, etc.?



inserting in different tables through one mysql query

Short answer
No you can't insert into more than one table using a single SQL statement.
Longer answer
You can achieve it by inserting into table1 and then setting up a trigger on table1 to insert data into tables 2 and 3 using a BLACKHOLE table to store your data transiently. 
Personally I would avoid this like the plague since it obscures how & where data is being inserted into the database from a given application. 
If I were you I would just write three separate INSERT statements in your php application and avoid trying to create a single query that inserts into multiple tables. It'll save you a great deal of pain!



As far as I know, PHP's mysql_query() function doesn't support multiple queries (insert, select, update etc), and as you are inserting into different tables with different fields/columns, you have to run a separate query for each. I don't think there is a way to do what you want.