user9203881
user9203881

Reputation: 115

Big execution time differences each refresh with pdo/mariadb

I suppose mariadb is working similarly to mysql, this is what I'm using, and I know there is a cache system.

My problem and what I don't understand, is that the pages that I refresh takes a long time to refresh, but the time is not constant at all. Details later.

On page A:

85% of the time, it takes ~7 seconds to execute.

10% of the time, it takes ~27 seconds.

5% of the time it takes under 1 second (when I refresh in very short intervals).

On page B:

80% of the time, it takes ~5 seconds.

Sometimes it's ~2.5 seconds.

Sometimes it's less than a second.

One time it has been >60 seconds, triggering an error.

My code is not changing, it's just observation and refreshing with F5.

Details:

I have a MyISAM table, that gets roughly 150k new rows ("insert") per day. I am looking to query this table every minutes ("select"). The max rows it could have at a time might range between 50,000,000 and 4,750,000,000... I'm using PHP to run the queries on the same server.

Structure I'm using currently:

CREATE TABLE `ticks` (
 `primary` int(11) NOT NULL AUTO_INCREMENT,
 `datetime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
 `pairs` text NOT NULL,
 `price` decimal(18,8) NOT NULL,
 `daily_volume` decimal(36,8) NOT NULL,
 PRIMARY KEY (`primary`),
 KEY `datetime` (`datetime`)
) ENGINE=MyISAM AUTO_INCREMENT=4007125 DEFAULT CHARSET=latin1

Data sample :

|primary | datetime            | pairs    | price          | volume           |
-------------------------------------------------------------------------------
|5810228 | 20/01/2018 21:34:02 | BTC_HUC  | 0.00002617     | 6.08607929       |
|5810213 | 20/01/2018 21:34:02 | BTC_BELA | 0.00002733     | 8.83542600       |
|5810224 | 20/01/2018 21:34:02 | BTC_FLDC | 0.00000374     | 12.72654326      |
|5810234 | 20/01/2018 21:34:02 | BTC_NMC  | 0.00037099     | 4.06446745       |
|5810219 | 20/01/2018 21:34:02 | BTC_CLAM | 0.00070798     | 13.65356478      |
|5810220 | 20/01/2018 21:34:02 | BTC_DASH | 0.07280004     | 423.88604591     |
|1706999 | 11/01/2018 17:09:01 | USDT_BTC | 13590.45341401 | 398959280.2620621|

I have created an index ("normal" index) on datetime.

The query on page A that takes 7 seconds to run with pdo, but ~0.0007 in phpmyadmin :

SELECT DISTINCT(pairs)
FROM ticks

Every heavy computations after this first query takes ~0.5 seconds total most of the time since I indexed datetime.

However, it sometimes takes between 25 and 35 times longer to run for unknown reasons. This is the query that is used (a loop runs it 100 times) :

SELECT datetime, price
FROM ticks
WHERE datetime <= DATE_SUB(NOW(),INTERVAL 1 MINUTE)
AND pairs = \''.$data['pairs'].'\'
ORDER BY datetime DESC
LIMIT 1

I'm not going further into explaining page B because this page is less critical for me and I'm comfortable with the avg execution time related to the number of operations made on this page. My only interrogation is the wide range of execution times that can occur here too.

Questions:

1-How can the execution time differences be so wide, how can I have my pages running in under 1 sec, as it happens sometimes ? My sql queries are extremely simple and fast on the database alone. I believe the db and the php server is located on the same machine.

In particular, I'm wondering why a query would run 10,000 slower with pdo than with phpmyadmin. 7/0.0007 being 10k, there has to be a huge problem here.

Indexing pairs is not changing anything.

2-Have you seen anything incorrect in what I explained that could lead to a fix and improvement of performances? Do you have particular advises to have increased performance in the presented case? For instance, I've been wondering if MyISAM was efficient in my case (I believe so).

Upvotes: 0

Views: 467

Answers (2)

rlanvin
rlanvin

Reputation: 6267

How can the execution time differences be so wide, how can I have my pages running in under 1 sec, as it happens sometimes ? My sql queries are extremely simple and fast on the database alone.

It's impossible to answer this question with any degree of certainty without analyzing your platform, monitoring the performance each component, reviewing the code and all the queries, etc. This is way beyond the scope of SO.

What can be said is:

  • It's unlikely that it has anything to do with PDO itself (or PHPMyAdmin for that matter)
  • It's typical of a concurrency problem - that is unless you have a server and a database dedicated to rendering "page A" only, other requests and queries happening at the same time can impact performances
  • MyISAM is notoriously bad at handling a large volume on insert because it uses table locking (in short, it locks all the table every time you make an insert). InnoDB use row based locking which would very probably be much more efficient with 150k writes a day. To quote the MySQL Documentation:

Table locking enables many sessions to read from a table at the same time, but if a session wants to write to a table, it must first get exclusive access, meaning it might have to wait for other sessions to finish with the table first. During the update, all other sessions that want to access this particular table must wait until the update is done.

Upvotes: 0

Rick James
Rick James

Reputation: 142306

There is essentially no reason to use MyISAM any more, especially for performance.

7 seconds is terrible for a page load. How much of that is MySQL actions? Add some timers in the code. This will find out which query is the slowest and let's improve it. (I would guess that one unnecessarily slow query is at the root of your problem.)

"~0.0007" smells like the Query Cache kicked in and it did not really execute the query. I ignore that.

With MyISAM, INSERTs block SELECTs. That could explain the troubles during the insert part of the day.

The table is confusing -- you have a TIMESTAMP (resolution to second), yet there is a "daily_volume" which sounds like a resolution to the "day".

I see TEXT. How long are the rows? If less than 255, use VARCHAR, not TEXT. That would allow you to add INDEX(pairs), which allowSELECT DISTINCT(pairs) FROM ticks to run a lot faster.

But, instead of that index, add INDEX(pairs, datetime) in order to make the second SELECT run much faster.

Shrinking the table size will help some in speed. (By some, I mean anywhere between 10% and 10x, depending on a lot of factors.)

Your decimal sizes are excessive. Find the worst (probably BRKA) and shrink the m,n of DECIMAL(m,n). Currently you are using 9 and 15 bytes for those two columns. You might consider FLOAT (4 bytes, ~7 significant digits) or DOUBLE (8 bytes, ~16 digits).

See my notes on converting to InnoDB . Be aware that the disk footprint might double or triple. (Yes this is an advantage of MyISAM.)

Consider whether some other column (or combination of columns) is unique. If you have such, jetison the column primary and make that column(s) the PRIMARY KEY. If it happens to be (pairs, datetime), then that will give a further performance boost to some queries.

"Indexing pairs is not changing anything." -- Since you can't index a TEXT column without using "prefixing" and prefixing is virtually useless, I am not surprised.

Could you show me a sample of the data? I am not familiar with what a "pair" is.

An index starting with TIMESTAMP or DATETIME is rarely useful; get rid of it unless you have another query that benefits from it.

As for the Query Cache -- size should be no more than 50M. Does the data not change for 23 hours of the day, then there is a flurry of inserts? This would be a good case for using the QC. (Most production servers are better off turning it OFF.) Going above 50M may slow down performance.

After you have addressed most of my suggestions, some other issues may bubble to the surface. That is, I expect you to come back with another Question to finish improving the performance for your app.

Upvotes: 1

Related Questions