Iterate between dates and INSERT values in a performant way
up vote
1
down vote
favorite
I have created a query that populates a datapoint with random values.
The logic is simple: Iterate between the START and END dates and INSERT random values.
I want from this query to be very perfomant. For example populate every second of a year with values (what with this code will last ages). I am new to SQL statements of this complexity and I dont know the pitfalls and of it.
Are there some hidden areas in my code that can be improved? If I replace the random function with just a hardcoded value will it cause much boost?
Is a loop with a lot of INSERT INTO
time consuming; Is there a better way to Insert (some kind of batch insert)?
DO $$
DECLARE --Variables
NODE_ID bigint := 11; -- The node id of the datapoint
TIMESTAMP_START TIMESTAMP := '2018-12-06 22:00:00';
TIMESTAMP_END TIMESTAMP := '2018-12-10 00:00:00';
TS_STEP INTERVAL := '30 minute';
MAX_VALUE integer := 100;
BEGIN
LOOP
EXIT WHEN TIMESTAMP_START > TIMESTAMP_END;
INSERT INTO datapoint_values (dp_id, ts, datatype, source, int_value, float_value)
VALUES (NODE_ID, TIMESTAMP_START, 2, 0, floor(random()*(MAX_VALUE+1)), 0);
TIMESTAMP_START := TIMESTAMP_START + TS_STEP;
END LOOP;
END $$;
performance sql datetime postgresql plpgsql
add a comment |
up vote
1
down vote
favorite
I have created a query that populates a datapoint with random values.
The logic is simple: Iterate between the START and END dates and INSERT random values.
I want from this query to be very perfomant. For example populate every second of a year with values (what with this code will last ages). I am new to SQL statements of this complexity and I dont know the pitfalls and of it.
Are there some hidden areas in my code that can be improved? If I replace the random function with just a hardcoded value will it cause much boost?
Is a loop with a lot of INSERT INTO
time consuming; Is there a better way to Insert (some kind of batch insert)?
DO $$
DECLARE --Variables
NODE_ID bigint := 11; -- The node id of the datapoint
TIMESTAMP_START TIMESTAMP := '2018-12-06 22:00:00';
TIMESTAMP_END TIMESTAMP := '2018-12-10 00:00:00';
TS_STEP INTERVAL := '30 minute';
MAX_VALUE integer := 100;
BEGIN
LOOP
EXIT WHEN TIMESTAMP_START > TIMESTAMP_END;
INSERT INTO datapoint_values (dp_id, ts, datatype, source, int_value, float_value)
VALUES (NODE_ID, TIMESTAMP_START, 2, 0, floor(random()*(MAX_VALUE+1)), 0);
TIMESTAMP_START := TIMESTAMP_START + TS_STEP;
END LOOP;
END $$;
performance sql datetime postgresql plpgsql
Do you care about the quality of the randomness, or do you just want some arbitrary values?
– 200_success
15 hours ago
@200_success If I had I would choose performance, so quality of randomness is a background feature. I want maximum perfomance that coulbe be pressed out of this code.
– Predicate
15 hours ago
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I have created a query that populates a datapoint with random values.
The logic is simple: Iterate between the START and END dates and INSERT random values.
I want from this query to be very perfomant. For example populate every second of a year with values (what with this code will last ages). I am new to SQL statements of this complexity and I dont know the pitfalls and of it.
Are there some hidden areas in my code that can be improved? If I replace the random function with just a hardcoded value will it cause much boost?
Is a loop with a lot of INSERT INTO
time consuming; Is there a better way to Insert (some kind of batch insert)?
DO $$
DECLARE --Variables
NODE_ID bigint := 11; -- The node id of the datapoint
TIMESTAMP_START TIMESTAMP := '2018-12-06 22:00:00';
TIMESTAMP_END TIMESTAMP := '2018-12-10 00:00:00';
TS_STEP INTERVAL := '30 minute';
MAX_VALUE integer := 100;
BEGIN
LOOP
EXIT WHEN TIMESTAMP_START > TIMESTAMP_END;
INSERT INTO datapoint_values (dp_id, ts, datatype, source, int_value, float_value)
VALUES (NODE_ID, TIMESTAMP_START, 2, 0, floor(random()*(MAX_VALUE+1)), 0);
TIMESTAMP_START := TIMESTAMP_START + TS_STEP;
END LOOP;
END $$;
performance sql datetime postgresql plpgsql
I have created a query that populates a datapoint with random values.
The logic is simple: Iterate between the START and END dates and INSERT random values.
I want from this query to be very perfomant. For example populate every second of a year with values (what with this code will last ages). I am new to SQL statements of this complexity and I dont know the pitfalls and of it.
Are there some hidden areas in my code that can be improved? If I replace the random function with just a hardcoded value will it cause much boost?
Is a loop with a lot of INSERT INTO
time consuming; Is there a better way to Insert (some kind of batch insert)?
DO $$
DECLARE --Variables
NODE_ID bigint := 11; -- The node id of the datapoint
TIMESTAMP_START TIMESTAMP := '2018-12-06 22:00:00';
TIMESTAMP_END TIMESTAMP := '2018-12-10 00:00:00';
TS_STEP INTERVAL := '30 minute';
MAX_VALUE integer := 100;
BEGIN
LOOP
EXIT WHEN TIMESTAMP_START > TIMESTAMP_END;
INSERT INTO datapoint_values (dp_id, ts, datatype, source, int_value, float_value)
VALUES (NODE_ID, TIMESTAMP_START, 2, 0, floor(random()*(MAX_VALUE+1)), 0);
TIMESTAMP_START := TIMESTAMP_START + TS_STEP;
END LOOP;
END $$;
performance sql datetime postgresql plpgsql
performance sql datetime postgresql plpgsql
edited 15 hours ago
asked 15 hours ago
Predicate
227213
227213
Do you care about the quality of the randomness, or do you just want some arbitrary values?
– 200_success
15 hours ago
@200_success If I had I would choose performance, so quality of randomness is a background feature. I want maximum perfomance that coulbe be pressed out of this code.
– Predicate
15 hours ago
add a comment |
Do you care about the quality of the randomness, or do you just want some arbitrary values?
– 200_success
15 hours ago
@200_success If I had I would choose performance, so quality of randomness is a background feature. I want maximum perfomance that coulbe be pressed out of this code.
– Predicate
15 hours ago
Do you care about the quality of the randomness, or do you just want some arbitrary values?
– 200_success
15 hours ago
Do you care about the quality of the randomness, or do you just want some arbitrary values?
– 200_success
15 hours ago
@200_success If I had I would choose performance, so quality of randomness is a background feature. I want maximum perfomance that coulbe be pressed out of this code.
– Predicate
15 hours ago
@200_success If I had I would choose performance, so quality of randomness is a background feature. I want maximum perfomance that coulbe be pressed out of this code.
– Predicate
15 hours ago
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Code Review Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f209422%2fiterate-between-dates-and-insert-values-in-a-performant-way%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Do you care about the quality of the randomness, or do you just want some arbitrary values?
– 200_success
15 hours ago
@200_success If I had I would choose performance, so quality of randomness is a background feature. I want maximum perfomance that coulbe be pressed out of this code.
– Predicate
15 hours ago