Does MySQL have any support for custom data types?
Simple answer: no
Is there anything remotely like this on any database engine? I mostly
use MySQL, but I am curious if this has ever been implemented, short
of making the application call a function like the INET_ATON function.
Oracle has CREATE TYPE
which is analogous to some degree to a OO class, including features like member functions and inheritance
Postgres has CREATE TYPE
which is a bit less like OO classes (no member functions or inheritance) but are incredibly flexible and useful, even allowing you to create new base types. There is also CREATE DOMAIN
which allows a form of inheritance or sub-typing and basically extends a base type with some constraints. Postgres also has quite a few interesting base types by default, eg inet and geometric types. In Postgres one can write an extension in C for a custom datatype, such as in this example here with base36 data type.
SQL Server has CREATE TYPE
which allows you to create a custom data type based on an existing system data type. For example I could create a type called SSN
which is basically defined as VARCHAR(11)
but this way I don't have to remember how big a field it is.
YOUR QUERY
SELECT post.postid, post.attach FROM newbb_innopost AS post WHERE post.threadid = 51506;
At first glance, that query should only touches 1.1597% (62510 out of 5390146) of the table. It should be fast given the key distribution of threadid 51506.
REALITY CHECK
No matter which version of MySQL (Oracle, Percona, MariaDB) you use, none of them can fight to one enemy they all have in common : The InnoDB Architecture.
CLUSTERED INDEX
Please keep in mind that the each threadid entry has a primary key attached. This means that when you read from the index, it must do a primary key lookup within the ClusteredIndex (internally named gen_clust_index). In the ClusteredIndex, each InnoDB page contains both data and PRIMARY KEY index info. See my post Best of MyISAM and InnoDB for more info.
REDUNDANT INDEXES
You have a lot of clutter in the table because some indexes have the same leading columns. MySQL and InnoDB has to navigate through the index clutter to get to needed BTREE nodes. You should reduced that clutter by running the following:
ALTER TABLE newbb_innopost
DROP INDEX threadid,
DROP INDEX threadid_2,
DROP INDEX threadid_visible_dateline,
ADD INDEX threadid_visible_dateline_index (`threadid`,`visible`,`dateline`,`userid`)
;
Why strip down these indexes ?
- The first three indexes start with threadid
threadid_2
and threadid_visible_dateline
start with the same three columns
threadid_visible_dateline
does not need postid since it's the PRIMARY KEY and it's embedded
BUFFER CACHING
The InnoDB Buffer Pool caches data and index pages. MyISAM only caches index pages.
Just in this area alone, MyISAM does not waste time caching data. That's because it's not designed to cache data. InnoDB caches every data page and index page (and its grandmother) it touches. If your InnoDB Buffer Pool is too small, you could be caching pages, invalidating pages, and removing pages all in one query.
TABLE LAYOUT
You could shave of some space from the row by considering importthreadid
and importpostid
. You have them as BIGINTs. They take up 16 bytes in the ClusteredIndex per row.
You should run this
SELECT importthreadid,importpostid FROM newbb_innopost PROCEDURE ANALYSE();
This will recommend what data types these columns should be for the given dataset.
CONCLUSION
MyISAM has a lot less to contend with than InnoDB, especially in the area of caching.
While you revealed the amount of RAM (32GB
) and the version of MySQL (Server version: 10.0.12-MariaDB-1~trusty-wsrep-log mariadb.org binary distribution, wsrep_25.10.r4002
), there are still other pieces to this puzzle you have not revealed
- The InnoDB settings
- The Number of Cores
- Other settings from
my.cnf
If you can add these things to the question, I can further elaborate.
UPDATE 2014-08-28 11:27 EDT
You should increase threading
innodb_read_io_threads = 64
innodb_write_io_threads = 16
innodb_log_buffer_size = 256M
I would consider disabling the query cache (See my recent post Why query_cache_type is disabled by default start from MySQL 5.6?)
query_cache_size = 0
I would preserve the Buffer Pool
innodb_buffer_pool_dump_at_shutdown=1
innodb_buffer_pool_load_at_startup=1
Increase purge threads (if you do DML on multiple tables)
innodb_purge_threads = 4
GIVE IT A TRY !!!
Best Answer
No matter what you choose, you should pick a convention and stick to it.
Here are my recommendations
BOOL
/BOOLEAN
If the data values are truly binary, use the
BOOL
/BOOLEAN
data type.CHAR
/VARCHAR
YES
/NO
works well when you have lazy UI developers that want to show the result with out translating it. The single character value (Y
/N
) works well also.The downside: Multinational applications will still have to translate the text. (eg
はい
/いいえ
)NUMBER(1)
For databases that do not support
bool
/boolean
I recommend using aNUMBER(1)
. This way, you can tell the UI developers that they will need to translate it into to the proper language. Make sure you document what1
and0
mean.enum
I do like the idea of using an
enum
to store the values. This would allow you to store a greater range (egsomewhatReflective
,mostlyReflective
)Proper datatype
I'm assuming the question is about data types whose values can be represented as binary. (eg true/false) This section is here to remind readers: Always pick the proper data type for your column (eg
DATE
for dates, etc.)The albedo of an object (relfectiveness) is measured as a dimensionless
NUMBER
whose value isBETWEEN 0 AND 1
. I would not use aBOOLEAN
data type for thereflective
column.