Abstract Objective Our aim was to evaluate the relationship between serum vitamin D levels before 20 weeks of pregnancy and the risk of gestational diabetes mellitus. Methods This study is a retrospective study. We analyzed the relationship between serum 25 (OH) D level before 20 weeks of pregnancy (first antenatal examination) and the risk of gestational diabetes mellitus. Age, parity and pre-pregnancy body mass index were used as confounding factors. 8468 pregnant women were enrolled in this study between January 2018 and March 2020 at the Obstetrics and Gynecology Hospital of Fudan University. Adjusted smoothing splinespline plots, subgroup analysis and multivariate logistic regression analysis was conducted to estimate the relative risk between 25(OH)D and gestational diabetes mellitus. Results After fully adjusting the confounding factors, serum vitamin D is a protective factor in gestational diabetes mellitus (OR = 0.90). Compared with vitamin D deficiency, vitamin D insufficiency (OR = 0.78), sufficience (OR = 0.82) are a protective factor for gestational diabetes mellitus. Conclusion Sufficience vitamin D before 20 weeks of pregnancy is a protective factor for gestational diabetes mellitus. Vitamin D > 20 ng/mL can reduce the risk of GDM, which is not much different from the effect of > 30 ng/mL. The protective effect of vitamin D is more significant in obese pregnant women.