SELECT COUNT(DISTINCT id_camada) FROM … equals 349 And it doesn't change significantly over time.
[]'s 2015-06-18 15:16 GMT-03:00 Igor Neyman <iney...@perceptron.com>: > > > > > *From:* pgsql-performance-ow...@postgresql.org [mailto: > pgsql-performance-ow...@postgresql.org] *On Behalf Of *Irineu Ruiz > *Sent:* Thursday, June 18, 2015 1:53 PM > *To:* pgsql-performance@postgresql.org > *Subject:* [PERFORM] How to calculate statistics for one column > > > > Hi, > > > > I have a table with irregular distribution based in a foreign key, like > you can see in the end of the e-mail. > > > > Sometimes, in simples joins with another tables with the same id_camada > (but not the table owner of the foreign key, the planner chooses a seq scan > instead of use the index with id_camada. > > If I do the join using also de table owner of the foreign key, then the > index is used. > > > > In the first case, querys with seq scan tahe about 30 seconds and with the > index take about 40 ms. > > > > When I increase the statistics of the column id_camada to 900, then > everything works using the index in both cases. > > My doubt is: there is a way to discovery the best statistics number for > this column or is a process of trial and error? > > > > id_camada;count(*) > > 123;10056782 > > 83;311471 > > 42;11316 > > 367;5564 > > 163;3362 > > 257;2100 > > 89;1725 > > 452;1092 > > 157;904 > > 84;883 > > 233;853 > > 271;638 > > 272;620 > > 269;548 > > 270;485 > > 455;437 > > 255;427 > > 32;371 > > 39;320 > > 31;309 > > 411;291 > > 91;260 > > 240;251 > > 162;250 > > 444;247 > > 165;227 > > 36;215 > > 236;193 > > 54;185 > > 53;175 > > 76;170 > > 412;153 > > 159;140 > > 160;139 > > 105;130 > > 59;117 > > 60;117 > > 267;115 > > 238;112 > > 279;111 > > 465;111 > > 5;107 > > 74;103 > > 243;98 > > 35;96 > > 68;82 > > 400;78 > > 391;75 > > 49;74 > > 124;68 > > 73;66 > > 260;64 > > 66;62 > > 168;60 > > 172;56 > > 4;54 > > 44;54 > > 384;53 > > 237;53 > > 390;52 > > 234;52 > > 387;51 > > 378;51 > > 148;50 > > 64;50 > > 379;47 > > 56;46 > > 52;46 > > 377;46 > > 443;46 > > 253;45 > > 97;45 > > 280;43 > > 77;43 > > 2;40 > > 376;39 > > 45;38 > > 235;36 > > 231;36 > > 413;36 > > 241;36 > > 232;34 > > 388;32 > > 101;32 > > 249;32 > > 99;32 > > 100;32 > > 69;32 > > 125;31 > > 166;30 > > 65;29 > > 433;29 > > 149;28 > > 96;27 > > 71;27 > > 98;26 > > 67;26 > > 386;25 > > 50;24 > > 21;24 > > 122;24 > > 47;24 > > 291;22 > > 287;22 > > 404;22 > > 70;22 > > 48;21 > > 63;21 > > 153;18 > > 13;18 > > 46;18 > > 262;18 > > 43;17 > > 72;17 > > 161;17 > > 344;15 > > 29;15 > > 439;14 > > 104;14 > > 119;13 > > 456;12 > > 434;12 > > 55;10 > > 3;10 > > 345;10 > > 286;10 > > 15;10 > > 141;9 > > 169;9 > > 258;9 > > 18;9 > > 158;9 > > 14;8 > > 94;8 > > 463;8 > > 218;8 > > 92;8 > > 170;8 > > 58;7 > > 17;7 > > 19;7 > > 6;7 > > 414;7 > > 10;7 > > 7;7 > > 22;7 > > 90;6 > > 430;6 > > 27;6 > > 195;6 > > 16;6 > > 223;6 > > 11;6 > > 242;6 > > 9;6 > > 26;5 > > 57;5 > > 82;5 > > 451;5 > > 61;5 > > 8;5 > > 445;5 > > 140;5 > > 431;5 > > 197;5 > > 20;5 > > 362;5 > > 24;5 > > 385;4 > > 23;4 > > 25;4 > > 62;4 > > 134;4 > > 150;4 > > 215;4 > > 217;4 > > 219;4 > > 220;4 > > 222;4 > > 224;4 > > 244;4 > > 284;4 > > 318;4 > > 389;4 > > 415;4 > > 449;4 > > 461;4 > > 93;3 > > 209;3 > > 136;3 > > 299;3 > > 188;3 > > 319;3 > > 264;3 > > 95;3 > > 337;3 > > 1;3 > > 221;3 > > 310;3 > > 143;2 > > 320;2 > > 321;2 > > 322;2 > > 324;2 > > 210;2 > > 302;2 > > 438;2 > > 303;2 > > 239;2 > > 330;2 > > 196;2 > > 447;2 > > 332;2 > > 333;2 > > 334;2 > > 307;2 > > 308;2 > > 309;2 > > 340;2 > > 341;2 > > 171;2 > > 190;2 > > 313;2 > > 193;2 > > 154;2 > > 294;2 > > 295;2 > > 250;2 > > 144;2 > > 311;1 > > 312;1 > > 314;1 > > 315;1 > > 316;1 > > 317;1 > > 51;1 > > 323;1 > > 325;1 > > 326;1 > > 327;1 > > 328;1 > > 329;1 > > 331;1 > > 335;1 > > 336;1 > > 338;1 > > 339;1 > > 342;1 > > 343;1 > > 186;1 > > 185;1 > > 354;1 > > 355;1 > > 356;1 > > 357;1 > > 359;1 > > 360;1 > > 361;1 > > 184;1 > > 363;1 > > 364;1 > > 366;1 > > 183;1 > > 369;1 > > 370;1 > > 182;1 > > 181;1 > > 180;1 > > 179;1 > > 380;1 > > 381;1 > > 382;1 > > 383;1 > > 178;1 > > 177;1 > > 176;1 > > 174;1 > > 30;1 > > 173;1 > > 392;1 > > 393;1 > > 155;1 > > 405;1 > > 407;1 > > 409;1 > > 151;1 > > 145;1 > > 12;1 > > 425;1 > > 138;1 > > 135;1 > > 103;1 > > 435;1 > > 437;1 > > 102;1 > > 440;1 > > 441;1 > > 442;1 > > 80;1 > > 448;1 > > 28;1 > > 226;1 > > 227;1 > > 228;1 > > 230;1 > > 225;1 > > 214;1 > > 216;1 > > 213;1 > > 212;1 > > 211;1 > > 208;1 > > 207;1 > > 206;1 > > 78;1 > > 245;1 > > 205;1 > > 204;1 > > 254;1 > > 203;1 > > 202;1 > > 201;1 > > 200;1 > > 199;1 > > 265;1 > > 198;1 > > 268;1 > > 194;1 > > 192;1 > > 273;1 > > 274;1 > > 275;1 > > 278;1 > > 191;1 > > 282;1 > > 75;1 > > 285;1 > > 189;1 > > 288;1 > > 289;1 > > 290;1 > > 187;1 > > 293;1 > > 296;1 > > 297;1 > > 300;1 > > 304;1 > > 305;1 > > 306;1 > > > > -- > > > > So what’s the result of: > > > > SELECT COUNT(DISTINCT id_camada) FROM … > > > > Does it change significantly over time? > > > > Regards, > > Igor Neyman > > > -- [image: E-mail] *Irineu Ruiz* Rua Helena, 275 - 12º Andar - Vila Olímpia 04552-050 - São Paulo - SP – (011) 2667-0708 www.rassystem.com.br – *iri...@rassystem.com.br <iri...@rassystem.com.br>*