I created a sample AWS Timestream database with one table:
Initially this had 400 sample rows, and the following query took ~0.3 seconds on average from the Timestream Console:
SELECT
make, COUNT(*) AS makeCount
FROM
"sampleDB"."IoTMulti"
WHERE
time BETWEEN TIMESTAMP '2021-11-30 02:47:15' AND TIMESTAMP '2022-12-30 22:47:55'
GROUP BY
make
However, after populating the table with 10,000,000 test records, the same query now takes ~10 seconds on average. This is quite bad.
Is there anything I can do to reduce the query time?
Here is the code I used to create more sample data:
const Promise = require('bluebird');
const { TimestreamWrite } = require('@aws-sdk/client-timestream-write');
const writeClient = new TimestreamWrite({});
const upsert = async ({ dimensions, metrics, time, version }) => {
const params = {
DatabaseName: 'sampleDB',
TableName: 'IoTMulti',
Records: [
{
Time: time,
MeasureValues: metrics
}
],
CommonAttributes: {
Dimensions: dimensions,
MeasureValueType: 'MULTI',
MeasureName: 'IoTMulti-stats',
Version: version || 1
}
};
const result = await writeClient.writeRecords(params);
return result;
};
const makes = [
'Birchfield',
'Alpha Sports',
'Ascort',
'Austin',
'Australian Six',
'Australis',
'Birchfield',
'Blade',
'Buchanan',
'Buckle',
'Bush Ranger',
'Caldwell Vale',
'Cheetah',
'Chrysler',
'Ford',
'FPV',
'Giocattolo',
'Goggomobil',
'Hartnett',
'Holden',
'HSV',
'Honda',
'Ilinga',
'Kaditcha',
'Leyland',
'Lloyd-Hartnett',
'Lonsdale',
'Mitsubishi',
'Morris',
'Nissan',
'Pellandini',
'Purvis Eureka',
'Shrike',
'Southern Cross',
'Statesman',
'Tarrant',
'Toyota',
'Volkswagen',
'Zeta'
];
(async () => {
const write = async () => {
const rows = [...Array(2500).keys()].map((index) => {
const minDate = new Date().getTime() - 1000 * 60 * 60 * 24 * 1;
const maxDate = new Date().getTime();
return {
dimensions: [
{ Name: 'fleet', Value: 'Test' },
{
Name: 'truck_id',
Value: Math.floor(Math.random() * 4132246625).toString()
},
{ Name: 'fuel_capacity', Value: '100' },
{ Name: 'load_capacity', Value: '1000' },
{
Name: 'make',
Value: makes[Math.floor(Math.random() * makes.length)]
},
{ Name: 'model', Value: Math.floor(Math.random() * 10000).toString() }
],
metrics: [
{
Name: 'fuel-reading',
Value: (Math.random() * 100).toString(),
Type: 'DOUBLE'
},
{
Name: 'load',
Value: Math.floor(Math.random() * 2000).toString(),
Type: 'DOUBLE'
},
{
Name: 'location',
Value: '25.7617° N, 80.1918° W',
Type: 'VARCHAR'
},
{
Name: 'speed',
Value: Math.floor(Math.random() * 100).toString(),
Type: 'DOUBLE'
}
],
time: Math.floor(
Math.random() * (maxDate - minDate + 1) + minDate
).toString(),
version: 1
};
});
await Promise.all(
rows.map(async (row) => {
await upsert(row);
})
);
};
await Promise.mapSeries([...Array(1000000).keys()], async (index) => {
console.log(`${index + 1}`);
await write();
});
})();