Retrieving closest date to input date












0















I have the following function:



DELIMITER $$
DROP FUNCTION IF EXISTS f_prevpricedate;
CREATE FUNCTION f_prevpricedate (id CHAR(8), startdate DATE)
RETURNS DATE

BEGIN

DECLARE prevpricedate DATE;

SELECT MAX(f.p_date) INTO prevpricedate
FROM fp_v2_fp_basic_prices AS f
WHERE f.fsym_id = id AND f.p_date<startdate;

RETURN prevpricedate;

END$$


Which basically just returns the closest date (previous date) to the input date. It runs extremely slow though, since the table is very large.



Anyone have any idea how to optimize this?










share|improve this question























  • Is fp_v2_fp_basic_prices.p_date indexed? What does explain select ... say?

    – Schwern
    Nov 14 '18 at 21:00











  • @Schwern I am not sure what indexed in this context means. I started using SQL 2 days ago.

    – Chris
    Nov 14 '18 at 21:03











  • Index are very important for efficient SQL. Your code is very impressive for day 2.

    – Schwern
    Nov 14 '18 at 21:12











  • @Schwern Thank you. I still feel like I struggle to really understand the ups and downs but I find your comment encouraging!

    – Chris
    Nov 14 '18 at 21:18
















0















I have the following function:



DELIMITER $$
DROP FUNCTION IF EXISTS f_prevpricedate;
CREATE FUNCTION f_prevpricedate (id CHAR(8), startdate DATE)
RETURNS DATE

BEGIN

DECLARE prevpricedate DATE;

SELECT MAX(f.p_date) INTO prevpricedate
FROM fp_v2_fp_basic_prices AS f
WHERE f.fsym_id = id AND f.p_date<startdate;

RETURN prevpricedate;

END$$


Which basically just returns the closest date (previous date) to the input date. It runs extremely slow though, since the table is very large.



Anyone have any idea how to optimize this?










share|improve this question























  • Is fp_v2_fp_basic_prices.p_date indexed? What does explain select ... say?

    – Schwern
    Nov 14 '18 at 21:00











  • @Schwern I am not sure what indexed in this context means. I started using SQL 2 days ago.

    – Chris
    Nov 14 '18 at 21:03











  • Index are very important for efficient SQL. Your code is very impressive for day 2.

    – Schwern
    Nov 14 '18 at 21:12











  • @Schwern Thank you. I still feel like I struggle to really understand the ups and downs but I find your comment encouraging!

    – Chris
    Nov 14 '18 at 21:18














0












0








0








I have the following function:



DELIMITER $$
DROP FUNCTION IF EXISTS f_prevpricedate;
CREATE FUNCTION f_prevpricedate (id CHAR(8), startdate DATE)
RETURNS DATE

BEGIN

DECLARE prevpricedate DATE;

SELECT MAX(f.p_date) INTO prevpricedate
FROM fp_v2_fp_basic_prices AS f
WHERE f.fsym_id = id AND f.p_date<startdate;

RETURN prevpricedate;

END$$


Which basically just returns the closest date (previous date) to the input date. It runs extremely slow though, since the table is very large.



Anyone have any idea how to optimize this?










share|improve this question














I have the following function:



DELIMITER $$
DROP FUNCTION IF EXISTS f_prevpricedate;
CREATE FUNCTION f_prevpricedate (id CHAR(8), startdate DATE)
RETURNS DATE

BEGIN

DECLARE prevpricedate DATE;

SELECT MAX(f.p_date) INTO prevpricedate
FROM fp_v2_fp_basic_prices AS f
WHERE f.fsym_id = id AND f.p_date<startdate;

RETURN prevpricedate;

END$$


Which basically just returns the closest date (previous date) to the input date. It runs extremely slow though, since the table is very large.



Anyone have any idea how to optimize this?







mysql






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 14 '18 at 20:59









ChrisChris

1039




1039













  • Is fp_v2_fp_basic_prices.p_date indexed? What does explain select ... say?

    – Schwern
    Nov 14 '18 at 21:00











  • @Schwern I am not sure what indexed in this context means. I started using SQL 2 days ago.

    – Chris
    Nov 14 '18 at 21:03











  • Index are very important for efficient SQL. Your code is very impressive for day 2.

    – Schwern
    Nov 14 '18 at 21:12











  • @Schwern Thank you. I still feel like I struggle to really understand the ups and downs but I find your comment encouraging!

    – Chris
    Nov 14 '18 at 21:18



















  • Is fp_v2_fp_basic_prices.p_date indexed? What does explain select ... say?

    – Schwern
    Nov 14 '18 at 21:00











  • @Schwern I am not sure what indexed in this context means. I started using SQL 2 days ago.

    – Chris
    Nov 14 '18 at 21:03











  • Index are very important for efficient SQL. Your code is very impressive for day 2.

    – Schwern
    Nov 14 '18 at 21:12











  • @Schwern Thank you. I still feel like I struggle to really understand the ups and downs but I find your comment encouraging!

    – Chris
    Nov 14 '18 at 21:18

















Is fp_v2_fp_basic_prices.p_date indexed? What does explain select ... say?

– Schwern
Nov 14 '18 at 21:00





Is fp_v2_fp_basic_prices.p_date indexed? What does explain select ... say?

– Schwern
Nov 14 '18 at 21:00













@Schwern I am not sure what indexed in this context means. I started using SQL 2 days ago.

– Chris
Nov 14 '18 at 21:03





@Schwern I am not sure what indexed in this context means. I started using SQL 2 days ago.

– Chris
Nov 14 '18 at 21:03













Index are very important for efficient SQL. Your code is very impressive for day 2.

– Schwern
Nov 14 '18 at 21:12





Index are very important for efficient SQL. Your code is very impressive for day 2.

– Schwern
Nov 14 '18 at 21:12













@Schwern Thank you. I still feel like I struggle to really understand the ups and downs but I find your comment encouraging!

– Chris
Nov 14 '18 at 21:18





@Schwern Thank you. I still feel like I struggle to really understand the ups and downs but I find your comment encouraging!

– Chris
Nov 14 '18 at 21:18












1 Answer
1






active

oldest

votes


















1














First, check what explain select ... says.



Are fp_v2_fp_basic_prices.fsym_id and fp_v2_fp_basic_prices.p_date indexed? An index allows the database to quickly match and compare rows without having to look at all of them. If there's no index it will have to compare every row in the table.



For example...



mysql> select count(*) from foo;                                                                                                                              
+----------+
| count(*) |
+----------+
| 3 |
+----------+

mysql> explain select max(this) from foo where this < 42;
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------------+
| 1 | SIMPLE | foo | NULL | ALL | NULL | NULL | NULL | NULL | 3 | 33.33 | Using where |
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------------+


This shows the query is doing a full table scan. It's using no key (index), the type of query is ALL, and it thinks it has to look at all 3 rows in the table.



mysql> alter table foo add index foo_this (this);
Query OK, 0 rows affected (0.07 sec)
Records: 0 Duplicates: 0 Warnings: 0

mysql> explain select max(this) from foo where this < 42;
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+------------------------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+------------------------------+
| 1 | SIMPLE | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | Select tables optimized away |
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+------------------------------+


This is after adding an index. Select tables optimized away tells us the optimizer has figured out it can use the index to optimize away the whole query.





In your case you're searching by two columns, fsym_id and p_date. MySQL will only use one index per table in a query. So even if you have an index on fsym_id and an index on p_date it will only use one. To make this query perform very well you need both in a single index.



alter table fp_v2_fp_basic_prices add index(p_date, fsym_id);


This will work for queries that only use p_date as well as queries which use both p_date + fsym_id together. So you don't need an index on just p_date. But it does not cover queries which only use fsym_id. See this answer for more detail.



See also:




  • https://dev.mysql.com/doc/refman/5.7/en/explain-output.html

  • https://dev.mysql.com/doc/refman/5.7/en/optimization-indexes.html

  • http://www.mysqltutorial.org/mysql-index/

  • https://www.tutorialspoint.com/mysql/mysql-indexes.htm






share|improve this answer


























  • I just checked. The table is indexed on the date I try to look up even.

    – Chris
    Nov 14 '18 at 21:22











  • @Chris Check fsym_id as well. And be sure to check what explain says, maybe add that to your answer. MySQL can only use one index per table, so you may have to create a composite index of both fsym_id and p_date. See stackoverflow.com/questions/1823685/…

    – Schwern
    Nov 14 '18 at 21:25











  • I use MySQL Workbench and it shows that there are two indexes and those are fsym_id and p_date.

    – Chris
    Nov 14 '18 at 21:28











  • Also it does not show when I use Explain Select since I call this function in another select statement. Basically I in my select statement I have: f_prevpricedate(p.fsym_id,p_date) AS Previous_Date

    – Chris
    Nov 14 '18 at 21:36













  • @Chris If there are two separate indexes it can only use one of them to partially cut down on the number of rows to scan. If you make one index for p_date, fsym_id that can replace the lone p_date index. To use explain copy your SELECT MAX(f.p_date) INTO prevpricedate ... out of its function and work with it as a single statement until you've got it optimized. If you're using MySQL 8 what you're trying to accomplish might be better done with a window function.

    – Schwern
    Nov 14 '18 at 21:42











Your Answer






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: "1"
};
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',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
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%2fstackoverflow.com%2fquestions%2f53308619%2fretrieving-closest-date-to-input-date%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














First, check what explain select ... says.



Are fp_v2_fp_basic_prices.fsym_id and fp_v2_fp_basic_prices.p_date indexed? An index allows the database to quickly match and compare rows without having to look at all of them. If there's no index it will have to compare every row in the table.



For example...



mysql> select count(*) from foo;                                                                                                                              
+----------+
| count(*) |
+----------+
| 3 |
+----------+

mysql> explain select max(this) from foo where this < 42;
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------------+
| 1 | SIMPLE | foo | NULL | ALL | NULL | NULL | NULL | NULL | 3 | 33.33 | Using where |
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------------+


This shows the query is doing a full table scan. It's using no key (index), the type of query is ALL, and it thinks it has to look at all 3 rows in the table.



mysql> alter table foo add index foo_this (this);
Query OK, 0 rows affected (0.07 sec)
Records: 0 Duplicates: 0 Warnings: 0

mysql> explain select max(this) from foo where this < 42;
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+------------------------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+------------------------------+
| 1 | SIMPLE | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | Select tables optimized away |
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+------------------------------+


This is after adding an index. Select tables optimized away tells us the optimizer has figured out it can use the index to optimize away the whole query.





In your case you're searching by two columns, fsym_id and p_date. MySQL will only use one index per table in a query. So even if you have an index on fsym_id and an index on p_date it will only use one. To make this query perform very well you need both in a single index.



alter table fp_v2_fp_basic_prices add index(p_date, fsym_id);


This will work for queries that only use p_date as well as queries which use both p_date + fsym_id together. So you don't need an index on just p_date. But it does not cover queries which only use fsym_id. See this answer for more detail.



See also:




  • https://dev.mysql.com/doc/refman/5.7/en/explain-output.html

  • https://dev.mysql.com/doc/refman/5.7/en/optimization-indexes.html

  • http://www.mysqltutorial.org/mysql-index/

  • https://www.tutorialspoint.com/mysql/mysql-indexes.htm






share|improve this answer


























  • I just checked. The table is indexed on the date I try to look up even.

    – Chris
    Nov 14 '18 at 21:22











  • @Chris Check fsym_id as well. And be sure to check what explain says, maybe add that to your answer. MySQL can only use one index per table, so you may have to create a composite index of both fsym_id and p_date. See stackoverflow.com/questions/1823685/…

    – Schwern
    Nov 14 '18 at 21:25











  • I use MySQL Workbench and it shows that there are two indexes and those are fsym_id and p_date.

    – Chris
    Nov 14 '18 at 21:28











  • Also it does not show when I use Explain Select since I call this function in another select statement. Basically I in my select statement I have: f_prevpricedate(p.fsym_id,p_date) AS Previous_Date

    – Chris
    Nov 14 '18 at 21:36













  • @Chris If there are two separate indexes it can only use one of them to partially cut down on the number of rows to scan. If you make one index for p_date, fsym_id that can replace the lone p_date index. To use explain copy your SELECT MAX(f.p_date) INTO prevpricedate ... out of its function and work with it as a single statement until you've got it optimized. If you're using MySQL 8 what you're trying to accomplish might be better done with a window function.

    – Schwern
    Nov 14 '18 at 21:42
















1














First, check what explain select ... says.



Are fp_v2_fp_basic_prices.fsym_id and fp_v2_fp_basic_prices.p_date indexed? An index allows the database to quickly match and compare rows without having to look at all of them. If there's no index it will have to compare every row in the table.



For example...



mysql> select count(*) from foo;                                                                                                                              
+----------+
| count(*) |
+----------+
| 3 |
+----------+

mysql> explain select max(this) from foo where this < 42;
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------------+
| 1 | SIMPLE | foo | NULL | ALL | NULL | NULL | NULL | NULL | 3 | 33.33 | Using where |
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------------+


This shows the query is doing a full table scan. It's using no key (index), the type of query is ALL, and it thinks it has to look at all 3 rows in the table.



mysql> alter table foo add index foo_this (this);
Query OK, 0 rows affected (0.07 sec)
Records: 0 Duplicates: 0 Warnings: 0

mysql> explain select max(this) from foo where this < 42;
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+------------------------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+------------------------------+
| 1 | SIMPLE | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | Select tables optimized away |
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+------------------------------+


This is after adding an index. Select tables optimized away tells us the optimizer has figured out it can use the index to optimize away the whole query.





In your case you're searching by two columns, fsym_id and p_date. MySQL will only use one index per table in a query. So even if you have an index on fsym_id and an index on p_date it will only use one. To make this query perform very well you need both in a single index.



alter table fp_v2_fp_basic_prices add index(p_date, fsym_id);


This will work for queries that only use p_date as well as queries which use both p_date + fsym_id together. So you don't need an index on just p_date. But it does not cover queries which only use fsym_id. See this answer for more detail.



See also:




  • https://dev.mysql.com/doc/refman/5.7/en/explain-output.html

  • https://dev.mysql.com/doc/refman/5.7/en/optimization-indexes.html

  • http://www.mysqltutorial.org/mysql-index/

  • https://www.tutorialspoint.com/mysql/mysql-indexes.htm






share|improve this answer


























  • I just checked. The table is indexed on the date I try to look up even.

    – Chris
    Nov 14 '18 at 21:22











  • @Chris Check fsym_id as well. And be sure to check what explain says, maybe add that to your answer. MySQL can only use one index per table, so you may have to create a composite index of both fsym_id and p_date. See stackoverflow.com/questions/1823685/…

    – Schwern
    Nov 14 '18 at 21:25











  • I use MySQL Workbench and it shows that there are two indexes and those are fsym_id and p_date.

    – Chris
    Nov 14 '18 at 21:28











  • Also it does not show when I use Explain Select since I call this function in another select statement. Basically I in my select statement I have: f_prevpricedate(p.fsym_id,p_date) AS Previous_Date

    – Chris
    Nov 14 '18 at 21:36













  • @Chris If there are two separate indexes it can only use one of them to partially cut down on the number of rows to scan. If you make one index for p_date, fsym_id that can replace the lone p_date index. To use explain copy your SELECT MAX(f.p_date) INTO prevpricedate ... out of its function and work with it as a single statement until you've got it optimized. If you're using MySQL 8 what you're trying to accomplish might be better done with a window function.

    – Schwern
    Nov 14 '18 at 21:42














1












1








1







First, check what explain select ... says.



Are fp_v2_fp_basic_prices.fsym_id and fp_v2_fp_basic_prices.p_date indexed? An index allows the database to quickly match and compare rows without having to look at all of them. If there's no index it will have to compare every row in the table.



For example...



mysql> select count(*) from foo;                                                                                                                              
+----------+
| count(*) |
+----------+
| 3 |
+----------+

mysql> explain select max(this) from foo where this < 42;
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------------+
| 1 | SIMPLE | foo | NULL | ALL | NULL | NULL | NULL | NULL | 3 | 33.33 | Using where |
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------------+


This shows the query is doing a full table scan. It's using no key (index), the type of query is ALL, and it thinks it has to look at all 3 rows in the table.



mysql> alter table foo add index foo_this (this);
Query OK, 0 rows affected (0.07 sec)
Records: 0 Duplicates: 0 Warnings: 0

mysql> explain select max(this) from foo where this < 42;
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+------------------------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+------------------------------+
| 1 | SIMPLE | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | Select tables optimized away |
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+------------------------------+


This is after adding an index. Select tables optimized away tells us the optimizer has figured out it can use the index to optimize away the whole query.





In your case you're searching by two columns, fsym_id and p_date. MySQL will only use one index per table in a query. So even if you have an index on fsym_id and an index on p_date it will only use one. To make this query perform very well you need both in a single index.



alter table fp_v2_fp_basic_prices add index(p_date, fsym_id);


This will work for queries that only use p_date as well as queries which use both p_date + fsym_id together. So you don't need an index on just p_date. But it does not cover queries which only use fsym_id. See this answer for more detail.



See also:




  • https://dev.mysql.com/doc/refman/5.7/en/explain-output.html

  • https://dev.mysql.com/doc/refman/5.7/en/optimization-indexes.html

  • http://www.mysqltutorial.org/mysql-index/

  • https://www.tutorialspoint.com/mysql/mysql-indexes.htm






share|improve this answer















First, check what explain select ... says.



Are fp_v2_fp_basic_prices.fsym_id and fp_v2_fp_basic_prices.p_date indexed? An index allows the database to quickly match and compare rows without having to look at all of them. If there's no index it will have to compare every row in the table.



For example...



mysql> select count(*) from foo;                                                                                                                              
+----------+
| count(*) |
+----------+
| 3 |
+----------+

mysql> explain select max(this) from foo where this < 42;
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------------+
| 1 | SIMPLE | foo | NULL | ALL | NULL | NULL | NULL | NULL | 3 | 33.33 | Using where |
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------------+


This shows the query is doing a full table scan. It's using no key (index), the type of query is ALL, and it thinks it has to look at all 3 rows in the table.



mysql> alter table foo add index foo_this (this);
Query OK, 0 rows affected (0.07 sec)
Records: 0 Duplicates: 0 Warnings: 0

mysql> explain select max(this) from foo where this < 42;
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+------------------------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+------------------------------+
| 1 | SIMPLE | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | Select tables optimized away |
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+------------------------------+


This is after adding an index. Select tables optimized away tells us the optimizer has figured out it can use the index to optimize away the whole query.





In your case you're searching by two columns, fsym_id and p_date. MySQL will only use one index per table in a query. So even if you have an index on fsym_id and an index on p_date it will only use one. To make this query perform very well you need both in a single index.



alter table fp_v2_fp_basic_prices add index(p_date, fsym_id);


This will work for queries that only use p_date as well as queries which use both p_date + fsym_id together. So you don't need an index on just p_date. But it does not cover queries which only use fsym_id. See this answer for more detail.



See also:




  • https://dev.mysql.com/doc/refman/5.7/en/explain-output.html

  • https://dev.mysql.com/doc/refman/5.7/en/optimization-indexes.html

  • http://www.mysqltutorial.org/mysql-index/

  • https://www.tutorialspoint.com/mysql/mysql-indexes.htm







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 14 '18 at 23:07

























answered Nov 14 '18 at 21:11









SchwernSchwern

90k17102232




90k17102232













  • I just checked. The table is indexed on the date I try to look up even.

    – Chris
    Nov 14 '18 at 21:22











  • @Chris Check fsym_id as well. And be sure to check what explain says, maybe add that to your answer. MySQL can only use one index per table, so you may have to create a composite index of both fsym_id and p_date. See stackoverflow.com/questions/1823685/…

    – Schwern
    Nov 14 '18 at 21:25











  • I use MySQL Workbench and it shows that there are two indexes and those are fsym_id and p_date.

    – Chris
    Nov 14 '18 at 21:28











  • Also it does not show when I use Explain Select since I call this function in another select statement. Basically I in my select statement I have: f_prevpricedate(p.fsym_id,p_date) AS Previous_Date

    – Chris
    Nov 14 '18 at 21:36













  • @Chris If there are two separate indexes it can only use one of them to partially cut down on the number of rows to scan. If you make one index for p_date, fsym_id that can replace the lone p_date index. To use explain copy your SELECT MAX(f.p_date) INTO prevpricedate ... out of its function and work with it as a single statement until you've got it optimized. If you're using MySQL 8 what you're trying to accomplish might be better done with a window function.

    – Schwern
    Nov 14 '18 at 21:42



















  • I just checked. The table is indexed on the date I try to look up even.

    – Chris
    Nov 14 '18 at 21:22











  • @Chris Check fsym_id as well. And be sure to check what explain says, maybe add that to your answer. MySQL can only use one index per table, so you may have to create a composite index of both fsym_id and p_date. See stackoverflow.com/questions/1823685/…

    – Schwern
    Nov 14 '18 at 21:25











  • I use MySQL Workbench and it shows that there are two indexes and those are fsym_id and p_date.

    – Chris
    Nov 14 '18 at 21:28











  • Also it does not show when I use Explain Select since I call this function in another select statement. Basically I in my select statement I have: f_prevpricedate(p.fsym_id,p_date) AS Previous_Date

    – Chris
    Nov 14 '18 at 21:36













  • @Chris If there are two separate indexes it can only use one of them to partially cut down on the number of rows to scan. If you make one index for p_date, fsym_id that can replace the lone p_date index. To use explain copy your SELECT MAX(f.p_date) INTO prevpricedate ... out of its function and work with it as a single statement until you've got it optimized. If you're using MySQL 8 what you're trying to accomplish might be better done with a window function.

    – Schwern
    Nov 14 '18 at 21:42

















I just checked. The table is indexed on the date I try to look up even.

– Chris
Nov 14 '18 at 21:22





I just checked. The table is indexed on the date I try to look up even.

– Chris
Nov 14 '18 at 21:22













@Chris Check fsym_id as well. And be sure to check what explain says, maybe add that to your answer. MySQL can only use one index per table, so you may have to create a composite index of both fsym_id and p_date. See stackoverflow.com/questions/1823685/…

– Schwern
Nov 14 '18 at 21:25





@Chris Check fsym_id as well. And be sure to check what explain says, maybe add that to your answer. MySQL can only use one index per table, so you may have to create a composite index of both fsym_id and p_date. See stackoverflow.com/questions/1823685/…

– Schwern
Nov 14 '18 at 21:25













I use MySQL Workbench and it shows that there are two indexes and those are fsym_id and p_date.

– Chris
Nov 14 '18 at 21:28





I use MySQL Workbench and it shows that there are two indexes and those are fsym_id and p_date.

– Chris
Nov 14 '18 at 21:28













Also it does not show when I use Explain Select since I call this function in another select statement. Basically I in my select statement I have: f_prevpricedate(p.fsym_id,p_date) AS Previous_Date

– Chris
Nov 14 '18 at 21:36







Also it does not show when I use Explain Select since I call this function in another select statement. Basically I in my select statement I have: f_prevpricedate(p.fsym_id,p_date) AS Previous_Date

– Chris
Nov 14 '18 at 21:36















@Chris If there are two separate indexes it can only use one of them to partially cut down on the number of rows to scan. If you make one index for p_date, fsym_id that can replace the lone p_date index. To use explain copy your SELECT MAX(f.p_date) INTO prevpricedate ... out of its function and work with it as a single statement until you've got it optimized. If you're using MySQL 8 what you're trying to accomplish might be better done with a window function.

– Schwern
Nov 14 '18 at 21:42





@Chris If there are two separate indexes it can only use one of them to partially cut down on the number of rows to scan. If you make one index for p_date, fsym_id that can replace the lone p_date index. To use explain copy your SELECT MAX(f.p_date) INTO prevpricedate ... out of its function and work with it as a single statement until you've got it optimized. If you're using MySQL 8 what you're trying to accomplish might be better done with a window function.

– Schwern
Nov 14 '18 at 21:42




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • 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%2fstackoverflow.com%2fquestions%2f53308619%2fretrieving-closest-date-to-input-date%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

Xamarin.iOS Cant Deploy on Iphone

Glorious Revolution

Dulmage-Mendelsohn matrix decomposition in Python