Iterate between dates and INSERT values in a performant way











up vote
1
down vote

favorite
1












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 $$;









share|improve this question
























  • 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

















up vote
1
down vote

favorite
1












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 $$;









share|improve this question
























  • 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















up vote
1
down vote

favorite
1









up vote
1
down vote

favorite
1






1





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 $$;









share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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




















  • 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

















active

oldest

votes











Your Answer





StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
});
});
}, "mathjax-editing");

StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "196"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















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






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Quarter-circle Tiles

build a pushdown automaton that recognizes the reverse language of a given pushdown automaton?

Mont Emei